美文网首页
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 做聊天汽泡的方法

    聊天汽泡实现的两种的方式:1、用CAShapeLayer和UIBezierPath创建一个不规则layer,然后把...

  • 汽泡味的幸福

    汽水爆炸的感觉,就好像你还在。 喜欢气泡在嘴里沸腾跳跃的感觉,就好像那瞬间可以忘记所有烦恼,只记得那瞬间的快乐。 ...

  • iOS 关于UIView布局约束 layout、layoutSu

    [概要]:iOS layout的相关方法: 1、layoutSubviews 这个方法,默认没有做任何事情,需要子...

  • iOS开发:聊天输入框的实现

    iOS开发:聊天输入框的实现 iOS开发:聊天输入框的实现

  • JPush 远程通知集成

    注:此文只现在已经不能适配iOS10了,iOS10推送采用了新的方法,做iOS9及以下的系统可读此篇文章; iOS...

  • iOS图片拉伸(resizableImage)

    返回一张受保护且被拉伸的图片 应用场景:聊天窗口的气泡 方法一(弃用): iOS 5.0以前使用(弃用)这个方法会...

  • 聊天的方法

    1,情绪感染理论: 聊天是一场个人力量的交锋,能力强的人会碾压能力弱的人,并将其置于自己的思维框架之下。为了使聊天...

  • iOS推送之远程推送(iOS Notification Of R

    注:此文只现在已经不能适配iOS10了,iOS10推送采用了新的方法,做iOS9及以下的系统可读此篇文章。 最近公...

  • iOS推送之本地推送(iOS Notification Of L

    注:此文只现在已经不能适配iOS10了,iOS10推送采用了新的方法,做iOS9及以下的系统可读此篇文章。 写此文...

  • 海盐蜜桃汽泡酒

    (一) 岑于给我打电话的时候我正在费力起开一瓶蜜桃味的泡泡汽酒,关系好的时候给他设置过一个专属来电显示,用的Owl...

网友评论

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

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