Effect:
1 Customize a view first
#import <UIKit/UIKit.h>
#define Kcalloutwidth 80.0 // bubble height
#define Kcalloutheight 95.0 // bubble width
#define Karrorheight // bottom distance height
@interface calloutcontentview: UIView
@end
2 Implementation Code
#import "CallOutContentView.h"
@implementation calloutcontentview
-(instancetype) initWithFrame: (cgrect) frame {
if (self = [super initwithframe: frame]) {
Self . BackgroundColor = [uicolor clearcolor];
}
return self ;
}
#pragma mark-draw rect
-(void) DrawRect: (cgrect) rect{
[self drawincontext:uigraphicsgetcurrentcontext()];
Self. Layer. Shadowcolor = [[uicolor clearcolor] cgcolor];
Self. Layer. Shadowopacity = 1.0;
Self. Layer. Shadowoffset = cgsizemake(0.0f, 0.0f);
}
-(void) Drawincontext: (cgcontextref) context
{
cgcontextsetlinewidth(context, 2.0);
Cgcontextsetfillcolorwithcolor(context, [uicolor colorwithred: 0.000 green:0.251 Blue:0.502 Alpha:1.000]. Cgcolor); Bubble Fill Color
[self getdrawpath: Context];
}
Bubble Background Drawing
-(void) Getdrawpath: (cgcontextref) context
{
cgrect rrect = self. Bounds;
cgfloat radius = (kcalloutheight - karrorheight)/ 2.0;
cgfloat midx = cgrectgetmidx(rrect);
cgfloat maxy = cgrectgetmaxy(rrect)- karrorheight; adjust the position from the bottom
cgfloat midy = maxy/2.0;
cgcontextsavegstate(context); // Save context 1
Cgcontextbeginpath(context);
// bottom triangle
Cgcontextmovetopoint(context, Midx + karrorheight, Maxy);
Cgcontextaddlinetopoint(context, MIDX, Maxy + karrorheight);
cgcontextaddlinetopoint(context, Midx- karrorheight, Maxy);
Cgcontextfillpath(context); // render triangles
cgcontextrestoregstate(context); // Restore context 1
Cgcontextaddarc(context, MIDX, Midy + 5, radius, 0, m_pi*2, 1 ); // draw a circle
Cgcontextfillpath(context); // render Circle
Cgcontextclosepath(context);
}
@end
3 Creating an instance
Calloutcontentview *calloutview = [[calloutcontentview alloc]initwithframe: CGRectMake(0, 0, kcalloutwidth, kcalloutheight)];
Original iOS draw round bubbles