我的问题很简单。我想为 collectionView 中的单元格设置动画。事实上,我想在单元格后面显示一个灰色背景并缩小里面的图像。
这与 Pinterest 的效果(几乎)相同:
我曾经在按钮上编写动画代码,但我从来没有在单元格上这样做过。例如,如何将单元格链接到 touchUpInside 或 TouchDown 操作?
最佳答案
如果你想在触摸单元格时开始动画,你可以实现didHighlightItemAt
。您可能想在 didUnhighlightItemAt
中反转它:
override func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
UIView.animate(withDuration: 0.5) {
if let cell = collectionView.cellForItem(at: indexPath) as? CustomCell {
cell.imageView.transform = .init(scaleX: 0.95, y: 0.95)
cell.contentView.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1)
}
}
}
override func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
UIView.animate(withDuration: 0.5) {
if let cell = collectionView.cellForItem(at: indexPath) as? CustomCell {
cell.imageView.transform = .identity
cell.contentView.backgroundColor = .clear
}
}
}
产生:
关于swift - 使用 Swift 3 按下时动画单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45651816/