UITableViewCell和UICollectionViewCell单击,点击弹簧动画扩展

问题描述 投票:0回答:1

以下是我的春季动画代码

 var transform = CGAffineTransform.identity
     transform = transform.scaledBy(x: 0.96, y: 0.96)
     UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
       self.transform = transform
     }, completion: nil)

上面的代码工作正常,每当我点击或单击我的UITableViewCell和UICollectionViewCell时,我都想创建扩展名,我想要这个Spring动画效果。

如何执行?

ios swift uitableview uicollectionviewcell uiviewanimation
1个回答
0
投票

您必须分别为每个UITableViewCellUICollectionViewCell编写两个扩展。

UITableViewCell扩展

extension UITableViewCell {
    func showAnimation()  {
        var transform = CGAffineTransform.identity
            transform = transform.scaledBy(x: 0.96, y: 0.96)
            UIView.animate(withDuration: 3.3, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
              self.transform = transform
        }, completion: nil)
    }
}

现在您可以在用户单击时调用showAnimation()

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.showAnimation()
 }

也对UIcollectionViewCell做同样的事情

© www.soinside.com 2019 - 2024. All rights reserved.