绘制矩形左侧边带尖尖的气泡

 尖尖在矩形左侧边

// 设置frame时需要保证高多20 宽多10
- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGFloat w = self.bounds.size.width;
    CGFloat h = self.bounds.size.height;
    
    CGFloat r = 10;
    CGFloat arrowH = 10;
    
    // 上下右的padding
    CGFloat padding = 10;
    
    CGContextBeginPath(context);
    
    // 左上角起始点
    CGContextMoveToPoint(context,r + arrowH, padding);
    
    // 右上角 x需要 -r
    CGContextAddLineToPoint(context,w - r - padding,padding);
    // 上下文、圆心x、圆心y、半径、起始弧度、终止弧度、顺时针还是逆时针
    // 当clockwise为0表示顺时针,从圆心水平向右为0弧度,向左为M_PI弧度,向下为M_PI_2,向上为-M_PI_2
    //    CGContextAddArc(CGContextRef  _Nullable c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)
    CGContextAddArc(context, w - r - padding, r + padding, r, -M_PI_2, 0, 0);
    
    // 右下角 y需要 -r
    CGContextAddLineToPoint(context,w - padding,h - padding - r);
    CGContextAddArc(context, w - padding - r, h - padding - r, r, 0, M_PI_2, 0);
    
    // 左下角 x需要 +r
    CGContextAddLineToPoint(context,r + arrowH,h - padding);
    CGContextAddArc(context,r + arrowH, h - padding - r, r, M_PI_2, M_PI, 0);
    
    //    // 尖尖
    //    CGContextAddLineToPoint(context,_arrowX - factor * arrowH,arrowH);
    //    CGContextAddLineToPoint(context,_arrowX,0);
    //    CGContextAddLineToPoint(context,_arrowX + factor * arrowH,arrowH);
    
CGFloat arrowW = 20;
    CGFloat arrowY = r + 5 + arrowW * 0.5;
    // 尖头指向左边的小尖尖
    CGContextAddLineToPoint(context,arrowH, arrowY + padding + arrowW * 0.5);
    CGContextAddLineToPoint(context,0, arrowY + padding);
    CGContextAddLineToPoint(context,arrowH, arrowY + padding - arrowW * 0.5);

    
    
    // 左上角 y需要 +r
    CGContextAddLineToPoint(context,arrowH,r + padding);
    CGContextAddArc(context,r + arrowH, r + padding, r, M_PI, -M_PI_2, 0);
    
    
    [self.bubbleColor setFill];
    [[UIColor clearColor] setStroke];
    
    //设置阴影
    //    CGContextSetShadowWithColor(CGContextRef context, CGSize offset,CGFloat blur, CGColorRef color)
    //    context表示要作画的内容
    //    offset表示阴影的位置
    //    blur表示阴影的模糊度
    //    color表示图形要填充的颜色
    
    CGColorRef shadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.3].CGColor;
    
    // 阴影偏移CGSizeMake(1, 0)
    CGContextSetShadowWithColor(context, CGSizeMake(1, 0), 10, shadowColor);
    
    CGContextDrawPath(context,kCGPathFillStroke);
}

猜你喜欢

转载自blog.csdn.net/allanGold/article/details/85061814