使用Swift 3按下动画单元格 [英] Animate cell when pressed using Swift 3

查看:94
本文介绍了使用Swift 3按下动画单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My problem is really simple. I would like to animate a cell within a collectionView. Indeed, I would like to show a grey background behind the cell and scale down the image inside.

(几乎)与Pinterest效果相同:

It would be (almost) the same effect than Pinterest:

我曾经在按钮上对该动画进行编码,但我从未在一个单元格上做到了。例如,如何将单元格链接到touchUpInside或TouchDown动作?

I used to code that animation on buttons, but I never did that on a cell. How can link a cell to a touchUpInside or TouchDown action for example ?

推荐答案

如果您要在触摸触摸屏时开始动画单元格,您可以实现 didHighlightItemAt 。您可能想在 didUnhighlightItemAt 中反转它:

If you want to start animation when you touch on the cell, you can implement didHighlightItemAt. You probably want to reverse it in 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 3按下动画单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆