[英]How to make a reusable cell for both TableView and CollectionView
[英]How to repeat animations in a collectionView or tableView cell?
我问题的简化版本是我试图在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的绘制中。 我能得到的最接近的动画仅执行一次,但不会循环。 我在网上发现了大量有关单执行动画的信息,但没有循环播放的信息。 任何协助将不胜感激!
弄清楚了:需要从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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.