swift开发中 图片的聊天气泡

本文介绍了使用Swift实现聊天应用中的气泡显示效果的方法。通过自定义UIView并利用CALayer进行图片遮罩处理,实现了消息气泡的动态拉伸及图片消息的精确布局。

话不多说,直接上代码:

func imageLayer(x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat) {

        self.customView = self.msgItem.view // 图片imageView

        self.customView.frame = CGRect(x: x + self.msgItem.insets.left, y: y + self.msgItem.insets.top, width: width, height: height)

        self.addSubview(self.customView)

        self.bubbleImage.frame = self.customView.frame// 设置气泡image frame 为图片image frame

        self.bubbleImage.image = UIImage(named:meBubbleStr)!.stretchableImage(withLeftCapWidth: 15, topCapHeight:15)

        let layer = self.bubbleImage.layer// 获取气泡image layer

        layer.frame = CGRect.init(origin: CGPoint.zero, size: self.bubbleImage.layer.frame.size)

        self.customView.layer.mask = layer

    }

 

文字发送时:聊天气泡拉伸

func setBubbleImage(imageName:String,withCapInsets:UIEdgeInsets,x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat)  {

        var image = UIImage.init(named: imageName)// 图片气泡

        image = image?.resizableImage(withCapInsets: withCapInsets)// 设置拉伸四周边界

        self.bubbleImage.image = image

        self.bubbleImage.frame = CGRect(x: x, y: y, width: width + self.msgItem.insets.left + self.msgItem.insets.right, height: height + self.msgItem.insets.top + self.msgItem.insets.bottom)

        self.addSubview(self.bubbleImage)

    }

 

转载于:https://my.oschina.net/u/2532095/blog/845773

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值