iOS开发iOS Developer

CoreGraphics 绘制气泡

2016-12-19  本文已影响66人  这样的我321

#import@interface CustomAnnotationView : UIView

@property (nonatomic,strong)NSString * subTitle;

@property (nonatomic,strong)NSString * title;

@end

#import "CustomAnnotationView.h"

@implementation CustomAnnotationView

- (instancetype)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];

if (self) {

self.backgroundColor = [UIColor clearColor];

}

return self;

}

- (void)drawRect:(CGRect)rect {

CGRect frame = rect;

frame.size.height = rect.size.height - 10;

CGFloat radius = 5.0;//圆角弧度

CGFloat minx = CGRectGetMinX(frame), midx = CGRectGetMidX(frame),maxx = CGRectGetMaxX(frame);

CGFloat miny = CGRectGetMinY(frame), maxy = CGRectGetMaxY(frame);

CGContextRef context = UIGraphicsGetCurrentContext();

//画箭头

CGContextMoveToPoint(context, midx+5, maxy);

CGContextAddLineToPoint(context, midx, maxy+10);

CGContextAddLineToPoint(context, midx-5, maxy);

//添加四个角的圆角弧度

CGContextAddArcToPoint(context, minx, maxy, minx, maxy-radius, radius);

CGContextAddArcToPoint(context, minx, miny, maxx-radius, miny, radius);

CGContextAddArcToPoint(context, maxx, miny, maxx, maxy-radius, radius);

CGContextAddArcToPoint(context, maxx, maxy, midx+5, maxy, radius);

CGContextAddLineToPoint(context, midx+5, maxy);

CGContextSetStrokeColorWithColor(context, COLOR_COLOR_CELL_BOTTOM_LINE.CGColor);

CGContextSetLineWidth(context, 1);

CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);

CGContextDrawPath(context, kCGPathFillStroke);

NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:12],NSForegroundColorAttributeName:COLOR_BLACK};

[_title drawAtPoint:CGPointMake(10, 10) withAttributes:attributes];

[_subTitle drawAtPoint:CGPointMake(10, 30) withAttributes:attributes];

}

- (void)setSubTitle:(NSString *)subTitle {

_subTitle = subTitle;

[self setNeedsDisplay];

}

- (void)setTitle:(NSString *)title {

_title = title;

[self setNeedsDisplay];

}

@end

上一篇下一篇

猜你喜欢

热点阅读