This article is about the iOS code draw bubbles, hand teach you to draw a bubble, other shapes please the reader to try their own, I hope you have fun!
speechbubbleview.m//demo////Created by mygame on 15/3/4.//Copyright (c) 2015 Mygame. All rights reserved.//#import "SpeechBubbleView.h" #import <CoreGraphics/CoreGraphics.h> #define Kpopuptriangleheigh 12#define kpopuptrianglewidth 22#define kborderoffset 0//0.5f@implementation SpeechBubbleView-( Instancetype) initWithFrame: (cgrect) frame{self = [super Initwithframe:frame]; if (self) {self.backgroundcolor = [uicolor Purplecolor]; } return self;} -(void) DrawRect: (cgrect) rect {[Super Drawrect:rect]; CGFloat vieww = rect.size.width; CGFloat VIEWH = rect.size.height; CGFloat strokewidth = 1; CGFloat Borderradius = 10; CGFloat offset = strokewidth + kborderoffset; Cgcontextref context = Uigraphicsgetcurrentcontext (); Cgcontextsetlinejoin (context, kcglinejoinround); Cgcontextsetlinewidth (context, strokewidth); Sets the brush width cgcontextsetstrokecolorwithcolor (context, [Uicolor Lightgraycolor]. Cgcolor); Set Brush faceColor Cgcontextsetfillcolorwithcolor (context, [Uicolor Lightgraycolor]. Cgcolor); Set Fill color//Draw triangle/*---/*
/*Draw the red part
* /Cgcontextbeginpath (context); Cgcontextmovetopoint (context, Borderradius+offset, viewh-kpopuptriangleheigh-offset); Cgcontextaddlinetopoint (Context, round ((vieww-kpopuptrianglewidth)/2.0f) + offset, Viewh-kpopuptriangleheigh-offset); Cgcontextaddlinetopoint (context, round (vieww/2.0f), viewh-offset); Cgcontextaddlinetopoint (context, round (vieww+kpopuptrianglewidth)/2.0f) +offset, Viewh-kpopuptriangleheigh-offset); //Draw the rest
/*
* /Cgcontextaddarctopoint (context, Vieww-offset, Viewh-kpopuptriangleheigh-offset, Vieww-offset, Kpopuptriangleheigh+offset, borderradius-strokewidth); Cgcontextaddarctopoint (context, Vieww-offset, offset, vieww-borderradius-offset, offset, borderradius-strokewidth); Cgcontextaddarctopoint (context, offset, offset, offset, borderradius+offset, borderradius-strokewidth); Cgcontextaddarctopoint (context, offset, viewh-kpopuptriangleheigh-offset, Borderradius+offset, Viewh-kpopuptriangleheigh-offset, borderradius-strokewidth); Cgcontextclosepath (context); Cgcontextdrawpath (context, kcgpathfillstroke);} @end
For Cgcontextaddarctopoint interface explanation please click here
IOS Paint bubbles (bubble)