聊天功能(三)--创建TextCell、ImageCell等
2018-06-07 本文已影响43人
Mage
根据上一篇内容创建好了ChatMessageCell为父类,分别创建ChatTextCell、ChatImageCell和ChatVoiceCell.
一、ChatTextCell.swift
import UIKit
import Cartography
public class ChatTextCell: ChatMessageCell {
public let contentLabel = UILabel.init()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
// 将MessageView设置成contentLabel,父类会设置其布局
super.init(style: style, reuseIdentifier: reuseIdentifier, messageView: contentLabel)
contentLabel.font = UIFont.systemFont(ofSize: 16)
contentLabel.numberOfLines = 0
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public var message: Message? {
didSet {
if let message = message {
contentLabel.text = message.comment
}
}
}
}
二、ChatImageCell.swift
import UIKit
import Cartography
public class ChatImageCell: ChatMessageCell {
public let contentImageView = UIImageView.init()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier, messageView: contentImageView, backgroundImageView: nil)
contentImageView.layer.masksToBounds = true
contentImageView.layer.cornerRadius = 10
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public var message: Message? {
didSet {
if let message = message {
let image: UIImage? = imageCache().object(forKey: message.comment as AnyObject) as? UIImage
if let image = image {
contentImageView.image = image
}else{
let img = UIImage.init(contentsOfFile: Bundle.main.path(forResource: message.comment, ofType: nil)!)!
let contentSize = self.minification(img.size, default: CGSize.init(width: 160, height: 320))
let image = self.scale(image:img, size: contentSize)
contentImageView.image = image
DispatchQueue.global().async {
if image != nil {
imageCache().setObject(image!, forKey: message.comment as AnyObject)
}
}
}
}
}
}
/// 缩放尺寸
func minification(_ size: CGSize, default defaultSize: CGSize) -> CGSize {
let multiple: CGFloat = min(defaultSize.width/size.width, defaultSize.height/size.height)
// 输入multiple大于1,说明没有标准尺寸大,不需要缩放
return multiple > 1 ? size : CGSize.init(width: ceil(size.width * multiple), height: ceil(size.height * multiple))
}
/// 缩放图片
func scale(image: UIImage, size: CGSize) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
if image.size == size {
return image
}
image.draw(in: CGRect.init(origin: CGPoint.zero, size: size))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage
}
}
三、ChatVoiceCell.swift
public class ChatVoiceCell: ChatMessageCell {
public let backImageView = UIImageView.init()
public let voiceView: VoiceView = VoiceView.init()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier, messageView: voiceView, backgroundImageView: backImageView)
self.tapGestureBlock = {[unowned self] (tap) in
if self.voiceView.isAnimating {
VoiceManager.shared.stopVoice()
self.voiceView.stopAnimating()
}else{
let isSuccess = VoiceManager.shared.playVoice(self.message!)
if isSuccess {
self.voiceView.startAnimating()
}else{
print("播放出错")
}
}
}
NotificationCenter.default.addObserver(self, selector: #selector(voicePlay(notify:)), name: Notification.Name.ChatVoicePlayNotification, object: nil)
}
@objc func voicePlay(notify: NSNotification){
let message = notify.object as? Message
if message == nil || message != self.message {
if self.voiceView.isAnimating {
self.voiceView.stopAnimating()
}
}
}
deinit {
NotificationCenter.default.removeObserver(self)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public var message: Message? {
didSet {
if let message = message {
voiceView.from = message.from
voiceView.duration = Int(message.comment) ?? 0
if VoiceManager.shared.message == message {
voiceView.startAnimating()
}
}
}
}
}
上一篇:聊天功能(二)--创建ChatViewCell
下一篇:聊天功能(四)--创建ChatViewController
Demo地址:MAChatTableViewDemo