Паш*_*хин 4 autolayout uicollectionview swift
当更新 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
Run Code Online (Sandbox Code Playgroud)
改变约束的函数:
func setinsetsForContainer(left: CGFloat, right: CGFloat, top:CGFloat, bottom:CGFloat?){
containerLeft.constant = left
containerRight.constant = -right
containerTop.constant = top
containerBottom.constant = -bottom!
}
Run Code Online (Sandbox Code Playgroud)
动画更新约束:
setinsetsForContainer(left: 20, right: 20, top:100, bottom:100)
UIView.animate(withDuration: 2) {
self.view.layoutIfNeeded()
}
Run Code Online (Sandbox Code Playgroud)
只需在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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1278 次 |
| 最近记录: |