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

 尖尖在矩形左侧边

// 设置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);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值