在选择时为 uicollectionview 单元格设置动画 [英] Animate uicollectionview cells on selection
问题描述
我已经创建了一个自定义布局,并像这样在 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屋!