如何在旋转时间内删除 UICollectionView 的淡入淡出动画?
How to remove fade animation of UICollectionView during rotation time?
我只是想在旋转期间调整 CollectionView 的动画,这样就不会有像这样的任何淡入淡出效果
视图的简单移动。
所以经过一番小搜索后,我发现我只通过
覆盖 UICollectionViewFlowLayout
的两个方法
override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return nil
}
override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return layoutAttributesForItem(at: itemIndexPath)
}
但后来又出现了一个问题:当我在两条消息之间收到消息时,比如延迟接收消息
蓝色消息不仅向下移动,而且蓝色消息的旧实例会在短时间内停留在原位,然后向下移动。但是如果我 return 到初始偏好都
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)
}
}
我只是想在旋转期间调整 CollectionView 的动画,这样就不会有像这样的任何淡入淡出效果
视图的简单移动。 所以经过一番小搜索后,我发现我只通过 覆盖 UICollectionViewFlowLayout
的两个方法 override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return nil
}
override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return layoutAttributesForItem(at: itemIndexPath)
}
但后来又出现了一个问题:当我在两条消息之间收到消息时,比如延迟接收消息
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)
}
}