如何在旋转时删除UICollectionView的淡入淡出动画?

时间:2018-02-08 11:42:49

标签: ios animation uicollectionview rotation uicollectionviewlayout

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

只是简单地移动视图。 所以在经过一次小小的搜索之后我发现我只是通过它实现了这个效果 覆盖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 个答案:

答案 0 :(得分:0)

我想我找到了解决方案。 1)我需要找出在CollectionView中插入的元素的IndexPath。 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)
}



  }