美文网首页
Swift 聊天气泡图片拉伸

Swift 聊天气泡图片拉伸

作者: 喵喵粉 | 来源:发表于2020-10-18 22:13 被阅读0次

1. 聊天气泡图片:

chatSend@2x.jpg
  1. 设置UIImageViewcontentModescaleToFill
  2. 拉伸图片2种方式
  • 代码:
/*
 UIEdgeInsetsMake(7, 4, 4, 14) :
 图片的上7,左4,下4,右14的区域内可拉伸,其他区域均不可拉伸                
 */
ivBg.image = image.resizableImage(withCapInsets: UIEdgeInsets(top: 7, left: 4, bottom: 4, right: 14), resizingMode: .stretch)
  • 在Assets中:

点击slicing

image.png 7F151F9A-93D6-46D1-85A6-EFF12FC62615.png

设置4个边距

A8417176-CC8C-4B60-B06E-33A2948CA82B.png D2A93801-8A14-4476-B31C-C5EC78A181A7.png

效果:

DBECF6F8-D7BD-4E55-B103-371E5E43FEA5.png image.png

2. 时间的显示隐藏

///计算每条消息是否显示时间
fileprivate func computeVisibleTime(models: [MessageModel]) {
    
    var lastTimeInterval: TimeInterval = 0
    var visible = true
    
    for (index, model) in models.enumerated() {
        
        if 0 == index {
            visible = true
            lastTimeInterval = getTimeIntervalFromDateStr(model.msgTime)
        } else {
            let nowTimeInterval = getTimeIntervalFromDateStr(model.msgTime)
            
            //相隔5分钟
            if nowTimeInterval - lastTimeInterval > 5*60 {
                visible = true
                lastTimeInterval = nowTimeInterval
            } else {
                visible = false
            }
        }
        
        model.msgTimeVisiable = visible
    }
}

///秒s级
fileprivate func getTimeIntervalFromDateStr(_ dataStr: String) -> TimeInterval {
    kDateFmt.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
    let date = kDateFmt.date(from: dataStr)
    return date?.timeIntervalSince1970 ?? 0
}

相关文章

网友评论

      本文标题:Swift 聊天气泡图片拉伸

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