使用Swift按动画单元格3
问题描述:
我的问题非常简单。我想动画一个collectionView中的单元格。的确,我想在单元格后面显示灰色背景并缩小内部图像。使用Swift按动画单元格3
这将是(几乎)比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
}
}
}
国债收益率:
+0
完美的事情!这正是我想要的!非常感谢你 ! – KevinB
答
你有几个选择。
您可以实现集合视图委托方法
collecdtionView(:didSelectItemAtIndexPath:)
并将代码放在那里。您可以将水龙头手势识别器附加到您的视图中,以便响应水龙头。
您可以创建一个自定义按钮并将图像安装到其中,然后像常规一样使用该按钮的IBAction方法。
答
试试这个:
在您的自定义UICollectionViewCell
,改变imageView
transform
当选择单元格,即
class CollectionViewCell: UICollectionViewCell
{
@IBOutlet weak var imageView: UIImageView!
override var isSelected: Bool{
didSet{
UIView.animate(withDuration: 2.0) {
self.imageView.transform = self.isSelected ? CGAffineTransform(scaleX: 0.9, y: 0.9) : CGAffineTransform.identity
}
}
}
}
我想劳克当我触地的动作,并释放时触摸的内心 – KevinB