简体   繁体   English

如何在collectionView或tableView单元格中重复动画?

[英]How to repeat animations in a collectionView or tableView cell?

The simplified version of my problem is I am trying to loop/repeat an animation on a button within collectionView cells, but the animation only is executed once. 我问题的简化版本是我试图在collectionView单元格内的按钮上循环/重复播放动画,但是动画只执行一次。 This is my animation block located within my collectionViewCell: 这是我位于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)
}

I have tried calling the animateGlowingCircle() from several different places: within the collectionViewController's viewDidLoad or cellForRowAt, as well as from the collectionView's draw. 我试过从几个不同的地方调用animateGlowingCircle():在collectionViewController的viewDidLoad或cellForRowAt中,以及在collectionView的绘制中。 The closest I can get is the animation only executes once but it doesn't loop. 我能得到的最接近的动画仅执行一次,但不会循环。 I've found tons of info online regarding single-executed animations, but nothing for looped. 我在网上发现了大量有关单执行动画的信息,但没有循环播放的信息。 Any assistance will be greatly appreciated! 任何协助将不胜感激!

Figured it out: the looping cell animation needs to be called from the collectionViewController's willDisplayCell method. 弄清楚了:需要从collectionViewController的willDisplayCell方法中调用循环单元动画。 So something like this: 所以像这样:

// 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM