美文网首页
iOS 做聊天汽泡的方法

iOS 做聊天汽泡的方法

作者: 5619c8b6c380 | 来源:发表于2017-02-25 10:57 被阅读57次

    聊天汽泡实现的两种的方式:1、用CAShapeLayer和UIBezierPath创建一个不规则layer,然后把你要设置汽泡的View的layer的mask设置创建的不规则layery就行了。 上代码:

    -(CAShapeLayer*)maskLayer:(UIView* )view{ 
        
        CGFloat viewWidth = CGRectGetWidth(view.frame);
        CGFloat viewHeight = CGRectGetHeight(view.frame);
        CGFloat rightSpace = 8.;
        CGFloat topSpace = 8.;
        CGPoint point1 = CGPointMake(0, 0);
        CGPoint point2 = CGPointMake(viewWidth-rightSpace, 0);
        CGPoint point3 = CGPointMake(viewWidth-rightSpace, topSpace);
        CGPoint point4 = CGPointMake(viewWidth, topSpace+2);
        CGPoint point5 = CGPointMake(viewWidth-rightSpace, topSpace+10.);
        CGPoint point6 = CGPointMake(viewWidth-rightSpace, viewHeight);
        CGPoint point7 = CGPointMake(0, viewHeight);
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:point1];
        [path addLineToPoint:point2];
        [path addLineToPoint:point3];
        [path addLineToPoint:point4];
        [path addLineToPoint:point5];
        [path addLineToPoint:point6];
        [path addLineToPoint:point7];
        [path closePath];
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.path = path.CGPath;
        return layer;
    }
    

    比如想做微信发图片那种功能

    UIImageView *bubbleBackgroundView = [[bubbleBackgroundView alloc]init];
    bubbleBackgroundView.frame = CGRectMake(0, 0, 200,100);
    bubbleBackgroundView.layer.mask=[self maskLayer: bubbleBackgroundView];
    

    效果图


    粘贴图片.png

    第二种方式刚很简单,需要UI切一个汽泡的图(其实总的来说是两种实现方式是一样,只不过是获取不规则的layer的方式不同而已)。

    UIImageView *maskImageView = [[UIImageView alloc]initWithFrame:self.bubbleBackgroundView.bounds];
             UIImage *image = [self imageNamed:@"chat_to_bg_white_boderBlock" ofBundle:@"RongCloud.bundle"];
            maskImageView.image = [image
                                      resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height * 0.8, image.size.width * 0.2,
                                                                                   image.size.height * 0.2, image.size.width * 0.8)];;
    
    self.bubbleBackgroundView.layer.mask=maskImageView.layer;
    

    效果图:1、UI切的汽泡图


    chat_from_bg_normal@2x.png

    2、做出来的效果图


    粘贴图片2.png

    注意:第一种是没有圆角哦,第二种有

    相关文章

      网友评论

          本文标题:iOS 做聊天汽泡的方法

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