为什么我的 UICollectionViewCells 在为超级视图约束更改设置动画时会更改 alpha 值?
Why do my UICollectionViewCells change alpha value when animating a superview constraint change?
我有一个包含多个子视图的超级视图,其中包括一个 UICollectionView。超级视图有一个布局约束,将其位置固定在屏幕底部。我想通过更改该约束然后在 UIViewController 上调用 self.view.layoutIfNeeded()
来隐藏视图。这工作正常,除了 UICollectionView 的单元格在动画开始时下降到 alpha = 0,然后在结束时变回 alpha = 1。
由于动画很快,所以不是很明显,但我不明白为什么会这样。没有其他子视图具有此行为,并且 UICollectionView 本身不会更改 alpha...只有单元格。
要隐藏我调用的超级视图:
entryOffset.constant = entryHeight.constant
UIView.animateWithDuration(5, animations: {
self.view.layoutIfNeeded()
}) { (finished) in
}
随着动画的缓慢速度(5 秒),很明显单元格完全隐藏...然后淡入。我已经打破了:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {}
你可以看到单元格正在重新加载(我也不明白,因为它只是上层约束发生了变化......而不是 UICollectionView 本身。
关于单元格的 alpha 值为什么会发生变化以及如何防止这种情况有任何想法吗?
我通过对流布局进行子类化使单元格的 alpha 值不变:
import UIKit
class NoFadeFlowLayout: UICollectionViewFlowLayout {
override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attrs = super.initialLayoutAttributesForAppearingItem(at: itemIndexPath)
attrs?.alpha = 1.0
return attrs
}
override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attrs = super.finalLayoutAttributesForDisappearingItem(at: itemIndexPath)
attrs?.alpha = 1.0
return attrs
}
}
我有一个包含多个子视图的超级视图,其中包括一个 UICollectionView。超级视图有一个布局约束,将其位置固定在屏幕底部。我想通过更改该约束然后在 UIViewController 上调用 self.view.layoutIfNeeded()
来隐藏视图。这工作正常,除了 UICollectionView 的单元格在动画开始时下降到 alpha = 0,然后在结束时变回 alpha = 1。
由于动画很快,所以不是很明显,但我不明白为什么会这样。没有其他子视图具有此行为,并且 UICollectionView 本身不会更改 alpha...只有单元格。
要隐藏我调用的超级视图:
entryOffset.constant = entryHeight.constant
UIView.animateWithDuration(5, animations: {
self.view.layoutIfNeeded()
}) { (finished) in
}
随着动画的缓慢速度(5 秒),很明显单元格完全隐藏...然后淡入。我已经打破了:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {}
你可以看到单元格正在重新加载(我也不明白,因为它只是上层约束发生了变化......而不是 UICollectionView 本身。
关于单元格的 alpha 值为什么会发生变化以及如何防止这种情况有任何想法吗?
我通过对流布局进行子类化使单元格的 alpha 值不变:
import UIKit
class NoFadeFlowLayout: UICollectionViewFlowLayout {
override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attrs = super.initialLayoutAttributesForAppearingItem(at: itemIndexPath)
attrs?.alpha = 1.0
return attrs
}
override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attrs = super.finalLayoutAttributesForDisappearingItem(at: itemIndexPath)
attrs?.alpha = 1.0
return attrs
}
}