简体   繁体   中英

Can't stop UIView animation in swift

This is my animation code:

UIView.animate(withDuration: 1, delay: 1, options: [.repeat], animations: {
        self.micButtonOuterRing?.frame = CGRect(x: (self.originalOuterRingFrame?.origin.x)! - 30, y: (self.originalOuterRingFrame?.origin.y)! - 30, width: (self.originalOuterRingFrame?.width)! + 60, height: (self.originalOuterRingFrame?.height)! + 60)
        self.micButtonOuterRing?.alpha = 0
        self.micButtonOuterRing?.layer.cornerRadius = (self.micButtonOuterRing?.frame.width)! / 2
    }, completion: { _ in
        self.micButtonOuterRing?.frame = self.originalOuterRingFrame!
        self.micButtonOuterRing?.alpha = 1
    })

I am trying to stop it when I press a button so I run the following:

@IBAction func micButtonPressed(_ sender: Any) {

    micButtonOuterRing?.layer.removeAllAnimations()

}

But the animation keeps going. I know the micButtonPressed function works because I added a print statement and it worked. Can somebody help?

Use UIPropertyAnimator instead; its designed to let you pause, stop , reverse, etc. and the syntax is almost exactly the same as the old UIViewAnimation Methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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