我正在爲我的應用程序實現一個簡單的信使,用戶可以在它們自己之間聊天。該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