ios - 如何在旋转期间删除 UICollectionView 的淡入淡出动画?

标签 ios animation uicollectionview rotation uicollectionviewlayout

我只是想在旋转期间调整 CollectionView 的动画,这样就不会出现像这样的淡入淡出效果 enter image description here

只需简单地移动 View 即可。 因此,经过一番小小的搜索后,我发现我只需通过以下方式即可实现该效果 重写 UICollectionViewFlowLayout 的两个方法

 override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {

return nil

}
 override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
      return layoutAttributesForItem(at: itemIndexPath) 

}

但是另一个问题出现了:当我在两条消息之间接收消息时,例如接收延迟消息 enter image description here蓝色消息不仅会向下移动,而且蓝色消息的旧实例也会在原地停留很短一段时间,然后就会向下移动。但如果我返回到最初的偏好

  override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {

return nil

}
 override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
      return nil 

}

消息以整齐的方式适当移动,但在旋转过程中再次出现褪色效果。

这件事让我很困惑,也许有人有任何线索,我怎样才能保持旋转动画而不褪色效果并使消息动画效果像普通消息运动一样。

最佳答案

我想我找到了解决方案。 1)我需要找出CollectionView中插入的元素的IndexPaths。 2)计算插入 IndexPath 后放置的下一个元素(当我在两条消息之间接收消息时,例如接收延迟消息) 3)将新的initialLayoutAttributesForAppearingItem和finalLayoutAttributesForDisappearingItem应用到上面派生的IndexPaths数组,对于其余元素,我们只应用该首选项

 override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {

return nil

}
    override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
  return layoutAttributesForItem(at: itemIndexPath) 

 }

这是代码

   override func prepare(forCollectionViewUpdates updateItems: [UICollectionViewUpdateItem]) {

    super.prepare(forCollectionViewUpdates: updateItems)
               insertingIndexPaths.removeAll()
    print("begining : \(insertingIndexPaths)")


    // create an array

    let fullAmountOfCells = collectionView?.numberOfItems(inSection: 0)
    print("number of items: \(fullAmountOfCells)")

    for update in updateItems {
        if let indexPath = update.indexPathAfterUpdate,
            update.updateAction == .insert {
            insertingIndexPaths.append(indexPath)
            print("Example if indexPath if for loop:\(indexPath)")
        }

   }
    let lastPathOfInsertingElement = insertingIndexPaths.last
    let differenceBetweenFullAmountAndLastInsertElement = fullAmountOfCells! - (lastPathOfInsertingElement?.item)! - 1

    if differenceBetweenFullAmountAndLastInsertElement > 0 {
        for numeric in 1...differenceBetweenFullAmountAndLastInsertElement {
            insertingIndexPaths.append(IndexPath(item: numeric + (lastPathOfInsertingElement?.item)!, section: 0))
        }
        print("True array to be modified with attributes:\(insertingIndexPaths)")

}
 }

override func finalizeCollectionViewUpdates() {
    super.finalizeCollectionViewUpdates()
  //  ChatLogController.orientation = UIDevice.current.orientation
//    print(  ChatLogController.orientation = UIDevice.current.orientation)
    insertingIndexPaths.removeAll()
    movingIndexPath.removeAll()
}



   override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    let attributes = super.initialLayoutAttributesForAppearingItem(at: itemIndexPath)

    if insertingIndexPaths.contains(itemIndexPath) {
        // attributes?.alpha = 0.0
        //attributes?.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)

        print("Process in initialLayoutAttributesForAppearingItem: \(itemIndexPath)")
           return attributes

    } else {
         print("Process in initialLayout set to nil: \(itemIndexPath)")
        return nil
    }


}








  override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attributes = super.initialLayoutAttributesForAppearingItem(at: itemIndexPath)

if insertingIndexPaths.contains(itemIndexPath) {
    // attributes?.alpha = 0.0
    //attributes?.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)


    return nil

} else {
    print("processing final layout and it to leyoutAttributeForItem(at: \(itemIndexPath)")
    return layoutAttributesForItem(at:itemIndexPath)
}



  }

关于ios - 如何在旋转期间删除 UICollectionView 的淡入淡出动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48684669/

相关文章:

ios - 在 iOS6 上的 UITextView 上使用 attributedText (NSAttributedString) 仅适用于段落样式或字体,但不能同时适用于两者

ios - NSURLCredential 认证只有一把 key

jquery - 使用 CSS 和/或 jQuery 动画化一个 div 以显示绝对定位的内容

javascript - Angular 动态组件动画离开不起作用

ios - 如何更改 iOS 中网格和列表的 UICollectionView 布局

ios - 如何在不移动图像的情况下将文本放置在图像下方?

objective-c - Objective-c 中的奇怪类型转换

ios - 如何在 iOS 中正确变形文本?

ios - CollectionView 嵌套在 Collectionview 中

ios - 水平滚动 UICollectionView 一直到右边/最后一个单元格