在选择时为uicollectionview单元设置动画 [英] Animate uicollectionview cells on selection
问题描述
我已经创建了一个customlayout并在layoutAttributesForItemAtIndexPath中设置了我的单元格位置属性,就像这样
I have created a customlayout and set my cells position attribute in layoutAttributesForItemAtIndexPath like this
attributes.center = CGPointMake((size.width/2 - 100) + 100, (size.height/2 - 150) +100);
我想在选中单元格时为其设置动画。复制我们使用 initialLayoutAttributesForAppearingItemAtIndexPath
&获得的动画类型 finalLayoutAttributesForDisappearingItemAtIndexPath
。
I would like to animate a cell when it is selected. replicating the kind of animations we get with the initialLayoutAttributesForAppearingItemAtIndexPath
& finalLayoutAttributesForDisappearingItemAtIndexPath
.
我想在选择和取消选择单元格时执行此操作。
I'd like to do this when a cell is selected and de-selected.
所以例如:
单元格A位于 0,0
。单元格B的位置 50,100
。如果我选择单元格B,我想将其设置为 0,0
。并同时将单元格A设置为 50,100
。基本上切换位置,但动画。
Cell A is in position 0,0
. Cell B is in position 50,100
. If I select cell B I'd like to animate it to 0,0
. and at the same time animate cell A to 50,100
. basically switching positions, but animated.
推荐答案
也许是一种不同的方法。只需覆盖collectionViewCell中的isSelected。
Maybe a different approach. Just override isSelected in the collectionViewCell.
override var isHighlighted: Bool {
didSet {
if isHighlighted {
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
// animate highlight
}, completion: nil)
} else {
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
// animate unHighligh
}, completion: nil)
}
}
}
在这篇文章中 UICollectionView:选择动画单元格大小更改,您可以看到有关如何设置大小更改动画的示例。
In this post UICollectionView: Animate cell size change on selection you can see an example on how to animate a size change.
这篇关于在选择时为uicollectionview单元设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!