【问题标题】:How to repeat animations in a collectionView or tableView cell?如何在 collectionView 或 tableView 单元格中重复动画?
【发布时间】:2019-02-20 03:56:20
【问题描述】:

我的问题的简化版本是我试图在 collectionView 单元格内的按钮上循环/重复动画,但动画只执行一次。这是位于我的 collectionViewCell 中的动画块:

func animateGlowingCircle() {
    UIView.animate(withDuration: 1.0, delay: 0, options: [.autoreverse, .repeat], animations: {
        self.glowCircle.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
    }, completion: nil)
}

我尝试从几个不同的地方调用 animateGlowingCircle():在 collectionViewController 的 viewDidLoad 或 cellForRowAt 中,以及在 collectionView 的绘图中。我能得到的最接近的是动画只执行一次,但它不会循环。我在网上找到了大量关于单次执行动画的信息,但没有关于循环的信息。任何帮助将不胜感激!

【问题讨论】:

    标签: ios swift uitableview uicollectionview ios-animations


    【解决方案1】:

    想通了:循环单元动画需要从 collectionViewController 的 willDisplayCell 方法中调用。所以是这样的:

    // In collectionViewController    
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        let timerCell = cell as! TimerCollectionViewCell
        timerCell.animateGlowingCircle()  
    }
    
    
    // In collectionViewCell (this is the same code posted with the question)
    func animateGlowingCircle() {
        UIView.animate(withDuration: 1.0, delay: 0, options: [.autoreverse, .repeat], animations: {
            self.glowCircle.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
        }, completion: nil)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      相关资源
      最近更新 更多