更新约束时如何避免 UICollectionView 中的淡入淡出动画
How to avoid fade animation in UICollectionView when update constraints
更新 UICollectionView 的约束(缩小或放大)时,
项目(按比例缩小或放大)带有淡入淡出的动画。
如何避免这种情况?
// activating constraints:
containerLeft = containerView.leftAnchor.constraint(equalTo: view.leftAnchor)
containerRight = containerView.rightAnchor.constraint(equalTo: view.rightAnchor)
containerTop = containerView.topAnchor.constraint(equalTo: view.topAnchor)
containerBottom = containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
containerLeft.isActive = true
containerRight.isActive = true
containerTop.isActive = true
containerBottom.isActive = true
改变约束的函数:
func setinsetsForContainer(left: CGFloat, right: CGFloat, top:CGFloat, bottom:CGFloat?){
containerLeft.constant = left
containerRight.constant = -right
containerTop.constant = top
containerBottom.constant = -bottom!
}
动画更新约束:
setinsetsForContainer(left: 20, right: 20, top:100, bottom:100)
UIView.animate(withDuration: 2) {
self.view.layoutIfNeeded()
}
只需在 UICollectionViewFlowLayout
中覆盖此方法
// disable fade animation in cells
override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attribute = super.initialLayoutAttributesForAppearingItem(at: itemIndexPath)
attribute?.alpha = 1
return attribute
}
override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attribute = super.finalLayoutAttributesForDisappearingItem(at: itemIndexPath)
attribute?.alpha = 1
return attribute
}
// disable fade animation in header/footer
override func initialLayoutAttributesForAppearingSupplementaryElement(ofKind elementKind: String, at elementIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attribute = super.initialLayoutAttributesForAppearingSupplementaryElement(ofKind: elementKind, at: elementIndexPath)
attribute?.alpha = 1
return attribute
}
override func finalLayoutAttributesForDisappearingSupplementaryElement(ofKind elementKind: String, at elementIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attribute = super.finalLayoutAttributesForDisappearingSupplementaryElement(ofKind: elementKind, at: elementIndexPath)
attribute?.alpha = 1
return attribute
}
更新 UICollectionView 的约束(缩小或放大)时, 项目(按比例缩小或放大)带有淡入淡出的动画。 如何避免这种情况?
// activating constraints:
containerLeft = containerView.leftAnchor.constraint(equalTo: view.leftAnchor)
containerRight = containerView.rightAnchor.constraint(equalTo: view.rightAnchor)
containerTop = containerView.topAnchor.constraint(equalTo: view.topAnchor)
containerBottom = containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
containerLeft.isActive = true
containerRight.isActive = true
containerTop.isActive = true
containerBottom.isActive = true
改变约束的函数:
func setinsetsForContainer(left: CGFloat, right: CGFloat, top:CGFloat, bottom:CGFloat?){
containerLeft.constant = left
containerRight.constant = -right
containerTop.constant = top
containerBottom.constant = -bottom!
}
动画更新约束:
setinsetsForContainer(left: 20, right: 20, top:100, bottom:100)
UIView.animate(withDuration: 2) {
self.view.layoutIfNeeded()
}
只需在 UICollectionViewFlowLayout
中覆盖此方法// disable fade animation in cells
override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attribute = super.initialLayoutAttributesForAppearingItem(at: itemIndexPath)
attribute?.alpha = 1
return attribute
}
override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attribute = super.finalLayoutAttributesForDisappearingItem(at: itemIndexPath)
attribute?.alpha = 1
return attribute
}
// disable fade animation in header/footer
override func initialLayoutAttributesForAppearingSupplementaryElement(ofKind elementKind: String, at elementIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attribute = super.initialLayoutAttributesForAppearingSupplementaryElement(ofKind: elementKind, at: elementIndexPath)
attribute?.alpha = 1
return attribute
}
override func finalLayoutAttributesForDisappearingSupplementaryElement(ofKind elementKind: String, at elementIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attribute = super.finalLayoutAttributesForDisappearingSupplementaryElement(ofKind: elementKind, at: elementIndexPath)
attribute?.alpha = 1
return attribute
}