iOS绘制聊天气泡

作者: CGPointZero | 来源:发表于2017-07-24 15:50 被阅读189次

一个很实用的干货,聊天气泡的绘制(亲测用于有横向适配的app中,旋转后仍可行)
效果大概是这样的:


思路是:
写个UIView的子类,并且设置背景为clearColor,在drawRect里实现绘制。

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

下面是绘制的部分:

-(void)drawRect:(CGRect)rect{
    
    CGFloat w=rect.size.width;
    CGFloat h=rect.size.height;
    UIBezierPath *path=[UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(5, h-4)];
    [path addArcWithCenter:CGPointMake(9, h-4)
                    radius:4
                startAngle:M_PI
                  endAngle:M_PI_2
                 clockwise:NO];
    [path addLineToPoint:CGPointMake(w-4, h)];
    [path addArcWithCenter:CGPointMake(w-4, h-4)
                    radius:4
                startAngle:M_PI_2
                  endAngle:0
                 clockwise:NO];
    [path addLineToPoint:CGPointMake(w, 4)];
    [path addArcWithCenter:CGPointMake(w-4, 4)
                    radius:4
                startAngle:0
                  endAngle:-M_PI_2
                 clockwise:NO];
    [path addLineToPoint:CGPointMake(9, 0)];
    [path addArcWithCenter:CGPointMake(9, 4)
                    radius:4
                startAngle:-M_PI_2
                  endAngle:-M_PI
                 clockwise:NO];
    [path addLineToPoint:CGPointMake(5, 15)];
    [path addLineToPoint:CGPointMake(0, 20)];
    [path addLineToPoint:CGPointMake(5, 25)];
    [path addLineToPoint:CGPointMake(5, h-4)];
    
    [path setLineWidth:0.2];
    [[UIColor lightGrayColor] setStroke];
    [path stroke];
    
    [[UIColor whiteColor] setFill];
    [path fill];
    
    [path closePath];
}

如果需要收发箭头方向不一样,那么只需要设置方向(左/右)作为入参即可。

-(void)drawRect:(CGRect)rect{
    
    CGFloat w=rect.size.width;
    CGFloat h=rect.size.height;
    UIBezierPath *path=[UIBezierPath bezierPath];
    if(_direction==SCMessageDirectionSend){
     
        [path moveToPoint:CGPointMake(0, h-4)];
        [path addArcWithCenter:CGPointMake(4, h-4)
                        radius:4
                    startAngle:M_PI
                      endAngle:M_PI_2
                     clockwise:NO];
        [path addLineToPoint:CGPointMake(w-9, h)];
        [path addArcWithCenter:CGPointMake(w-9, h-4)
                        radius:4
                    startAngle:M_PI_2
                      endAngle:0
                     clockwise:NO];
        [path addLineToPoint:CGPointMake(w-5, 25)];
        [path addLineToPoint:CGPointMake(w, 20)];
        [path addLineToPoint:CGPointMake(w-5, 15)];
        
        [path addLineToPoint:CGPointMake(w-5, 4)];
        [path addArcWithCenter:CGPointMake(w-9, 4)
                        radius:4
                    startAngle:0
                      endAngle:-M_PI_2
                     clockwise:NO];
        [path addLineToPoint:CGPointMake(4, 0)];
        [path addArcWithCenter:CGPointMake(4, 4)
                        radius:4
                    startAngle:-M_PI_2
                      endAngle:-M_PI
                     clockwise:NO];
        [path addLineToPoint:CGPointMake(0, h-4)];
        
    }else{
        
        [path moveToPoint:CGPointMake(5, h-4)];
        [path addArcWithCenter:CGPointMake(9, h-4)
                        radius:4
                    startAngle:M_PI
                      endAngle:M_PI_2
                     clockwise:NO];
        [path addLineToPoint:CGPointMake(w-4, h)];
        [path addArcWithCenter:CGPointMake(w-4, h-4)
                        radius:4
                    startAngle:M_PI_2
                      endAngle:0
                     clockwise:NO];
        [path addLineToPoint:CGPointMake(w, 4)];
        [path addArcWithCenter:CGPointMake(w-4, 4)
                        radius:4
                    startAngle:0
                      endAngle:-M_PI_2
                     clockwise:NO];
        [path addLineToPoint:CGPointMake(9, 0)];
        [path addArcWithCenter:CGPointMake(9, 4)
                        radius:4
                    startAngle:-M_PI_2
                      endAngle:-M_PI
                     clockwise:NO];
        [path addLineToPoint:CGPointMake(5, 15)];
        [path addLineToPoint:CGPointMake(0, 20)];
        [path addLineToPoint:CGPointMake(5, 25)];
        [path addLineToPoint:CGPointMake(5, h-4)];
    }
    [_bgColor setFill];
    [path fill];
    
    [path setLineWidth:0.4];
    [[UIColor colorWithWhite:0.8 alpha:0.8] setStroke];
    [path stroke];
    
    [path closePath];
}
-(void)setDirection:(SCMessageDirection)direction{
    
    _direction=direction;
    [self setNeedsDisplay];
}

相关文章

网友评论

本文标题:iOS绘制聊天气泡

本文链接:https://www.haomeiwen.com/subject/ssephxtx.html