如何在集合视图或表视图单元格中重复动画?



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

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 的绘制中。 我能得到的最接近的是动画只执行一次,但它不会循环。 我在网上找到了大量关于单次执行动画的信息,但没有循环的信息。 任何帮助将不胜感激!

想通了:循环单元格动画需要从集合ViewController的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)
}

最新更新