iOS 做聊天汽泡的方法

聊天汽泡实现的两种的方式: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 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,195评论 4 61
  • 转载:http://www.jianshu.com/p/32fcadd12108 每个UIView有一个伙伴称为l...
    F麦子阅读 11,469评论 0 13
  • 看图成文-希望 今天的作文是由三张图联想写一篇文章,感觉像是回到小学时代的看图作文。 这三张图放在一起使我联想起“...
    温墨青蛙阅读 1,482评论 0 0
  • 又有点咳嗽迹象了,我总是喉咙一痒就有点想咳嗽了,然后我又买了盒含片,只要嘴巴里没味道了,我就含,抑制在牢笼里,哈哈...
    1大太阳阅读 1,163评论 0 1
  • 每一种爱都值得被珍惜,不知道青春的你,有没有那么一个人错过了彼此,匆匆分离在两个城市,最后的结局就是祝福彼此,每次...
    半糖青柠阅读 3,811评论 1 1