swift - 使用 Swift 3 按下时动画单元格

标签 swift animation swift3 uicollectionview cell

我的问题很简单。我想为 collectionView 中的单元格设置动画。事实上,我想在单元格后面显示一个灰色背景并缩小里面的图像。

这与 Pinterest 的效果(几乎)相同:

enter image description here

我曾经在按钮上编写动画代码,但我从来没有在单元格上这样做过。例如,如何将单元格链接到 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
        }
    }
}

产生:

demo

关于swift - 使用 Swift 3 按下时动画单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45651816/

相关文章:

ios - 使用 Swift 3 和 Realm 同步 Apple Watch 和 iPhone

swift - UIKeyCommand discoveryabilityTitle 已弃用

ios - 在 TableViewControllers 和 ViewControllers/Swift 之间切换数据

ios - 在 UITableViewCell 上重新渲染 subview 的最佳实践

css - 单击按钮时如何为动态变化的 div 设置动画

android - 如何为在 Android 中绘制的图形设置动画

ios - 从 nib UIView 加载未使用的 Swift 3

c# - WPF 动画 : How do i make it slide in?

ios - 使用 Swift 3 在 JSQMessagesViewController 中读取未读消息

ios - 自定义 uitableViewCell