iOS 绘制气泡

2018-03-22  本文已影响76人  武一顶顶
351521699256_.pic.jpg
CGContextAddArcToPoint(context, x1, y1, x2, y2, r)函数使用
----------------------------------------------------------
CGContextBeginPath(context);
CGContextMoveToPoint(context, Ax, Ay);
CGContextAddArcToPoint(context, Bx, By, Cx, Cy, r);

----------------------------------------------------------
****(误区)****:     B点坐标不能写成E点坐标

#import "SpeechBubbleView.h"
#import <CoreGraphics/CoreGraphics.h>

@implementation SpeechBubbleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGFloat viewW = rect.size.width;
    CGFloat viewH = rect.size.height;
    
    CGFloat strokeWidth = 0.1;
    CGFloat borderRadius = 10;
    CGFloat offset = strokeWidth + kBorderOffset;
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineJoin(context, kCGLineJoinRound); //
    CGContextSetLineWidth(context, strokeWidth); // 设置画笔宽度
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); // 设置画笔颜色
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); // 设置填充颜色
    
        // 画三角形
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, 0, borderRadius);
        CGContextAddLineToPoint(context, 0, viewH);

        // 画其余部分
        CGContextAddArcToPoint(context, viewW, viewH, viewW,viewH - borderRadius, borderRadius);
        CGContextAddArcToPoint(context, viewW, 0, viewW-borderRadius, 0, borderRadius);
        CGContextAddArcToPoint(context, 0, 0, 0, borderRadius, borderRadius);

        CGContextClosePath(context);
        CGContextDrawPath(context, kCGPathFillStroke);
}

@end
上一篇 下一篇

猜你喜欢

热点阅读