我正在为我的应用程序实现一个简单的信使,用户可以在它们自己之间聊天。该Messenger是基于UICollectionView(JSQMessagesViewController),其中每个消息由一个UICollectionView行表示。每封邮件还有一个顶部标签,用于显示邮件的发送时间。这个标签最初是隐藏的(height = 0),当用户点击特定的消息(行)时,通过相应地设置高度来显示标签。 (高度= 25)JSQMessagesView动画行高变化的问题
我面临的问题是显示所述标签的实际动画。 (高度变化)。行的一部分在达到其位置之前将行下方的行覆盖几个像素。同样,当隐藏标签时,动画首先将高度设置为零,然后文本淡出覆盖部分消息,该消息看起来非常糟糕。
所以基本上我试图实现的是摆脱这两个前面提到的问题。
代码:
override func collectionView(_ collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForCellTopLabelAt indexPath: IndexPath!) -> CGFloat {
if indexPath == indexPathTapped {
return 25
}
let messageCurrent = messages[indexPath.item]
let messagePrev: JSQMessage? = indexPath.item - 1 >= 0 ? messages[indexPath.item - 1] : nil
if messageCurrent.senderId == messagePrev?.senderId || messagePrev == nil {
return 0
}
else{
return 25
}
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, didTapMessageBubbleAt indexPath: IndexPath!) {
if let indexPathTapped = indexPathTapped, indexPathTapped == indexPath {
self.indexPathTapped = nil
}
else{
indexPathTapped = indexPath
}
collectionView.reloadItems(at: [indexPath])
// UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveLinear, animations: {
// collectionView.performBatchUpdates({
// collectionView.reloadItems(at: [indexPath])
// }, completion: nil)
// }, completion: nil)
}
演示:(很抱歉的质量)
我真的很感激,如果有人可以帮助我,因为我已经花了几个小时试图找出没有得到任何地方。
预先感谢您!
编辑:
我尝试作为继@jamesk提出的解决方案:
override func collectionView(_ collectionView: JSQMessagesCollectionView!, didTapMessageBubbleAt indexPath: IndexPath!) {
if let indexPathTapped = indexPathTapped, indexPathTapped == indexPath {
self.indexPathTapped = nil
}
else{
indexPathTapped = indexPath
}
UIView.animate(withDuration: 0.25) {
collectionView.performBatchUpdates(nil)
}
}
,并覆盖apply
JSQMessagesCollectionViewCell
的:
extension JSQMessagesCollectionViewCell {
override open func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
super.apply(layoutAttributes)
layoutIfNeeded()
}
}
但是这些变化导致:
我也试图与无效的布局的第二溶液:
override func collectionView(_ collectionView: JSQMessagesCollectionView!, didTapMessageBubbleAt indexPath: IndexPath!) {
if let indexPathTapped = indexPathTapped, indexPathTapped == indexPath {
self.indexPathTapped = nil
}
else{
indexPathTapped = indexPath
}
var paths = [IndexPath]()
let itemsCount = collectionView.numberOfItems(inSection: 0)
for i in indexPath.item...itemsCount - 1 {
paths.append(IndexPath(item: i, section: 0))
}
let context = JSQMessagesCollectionViewFlowLayoutInvalidationContext()
context.invalidateItems(at: paths)
UIView.animate(withDuration: 0.25) {
self.collectionView?.collectionViewLayout.invalidateLayout(with: context)
self.collectionView?.layoutIfNeeded()
}
}
这导致了以下内容:
我试过了你提出的两种解决方案,但是他们都没有工作(请看编辑问题)。你有什么想法出了什么问题? – user3559787
我尝试过这些东西,但它仍然无法正常工作。 – user3559787
我认为最简单的方法就是,如果你只是提供一个链接到你的例子(你已经有)。我会看到我做了什么不同,并重新上传到这里。 – user3559787