[英]Trouble animating the selected collection view cell when tapped
This is a simple question and I can't seem to solve the problem.这是一个简单的问题,我似乎无法解决问题。
I have a list of cells in a collection view and when I tap it I just want the cell to highlight for a bit then disappear just an animation for tapping.我在集合视图中有一个单元格列表,当我点击它时,我只想让单元格突出显示一点,然后消失只是一个点击动画。
I've tried changing the background in my didSelectItemAt but it seemed to be highlighting the wrong cell.我已经尝试在我的 didSelectItemAt 中更改背景,但它似乎突出显示了错误的单元格。
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
UIView.animate(withDuration: 0.5, animations: {
cell?.backgroundColor = .lightGray
}, completion: {
(value: Bool) in
cell?.backgroundColor = .white
})
}
Also tried multiple things I found from stack and it wasn't working.还尝试了我从堆栈中找到的多种东西,但它不起作用。
BackgroundColor is not animatable for any cell. BackgroundColor 不能为任何单元格设置动画。 One solution is to set backgroundView color and animate its alpha.一种解决方案是设置 backgroundView 颜色并为其 alpha 设置动画。 You could also insert your own views as backgrounds and animate their alphas in opposite directions to create the change.您还可以插入您自己的视图作为背景,并在相反方向上为它们的 alpha 设置动画以创建更改。
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt
indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
UIView.animate(withDuration: 0.5, animations: {
cell?.backgroundColor = UIColor.lightGray.cgColor
}, completion: {
(value: Bool) in
cell?.layer.backgroundColor = UIColor.white.cgColor
})
}
You can show that with the help of alpha
property as suggested您可以按照建议在alpha
属性的帮助下显示
UIView.animate(withDuration: 0.5, animations: { cell.backgroundColor = .darkGray cell.alpha = 0.7 }, completion: { (value: Bool) in cell.alpha = 1 }) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.