0

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?

OverD
  • 2,612
  • 2
  • 14
  • 29
N. Chalifour
  • 103
  • 10

1 Answers1

1

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.

Josh Homann
  • 15,933
  • 3
  • 30
  • 33
  • i did used the `UIPropertyAnimator` but now the animation does't repeat, this is my code: `self.micRingAnimation = UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1, delay: 1, options: [.repeat], animations: {}, completion: { _ in }` – N. Chalifour Nov 25 '17 at 16:44
  • if you want to use repeat then set UIView.setAnimationRepeatCount( Float.greatestFiniteMagnitute) in the animation block, alternatively wrap the animation call in a function and call that function in the completion handler to kick off a new animation when the current one finishes. – Josh Homann Nov 25 '17 at 16:48
  • its really weird... the animation still doesn't stop. Does it have anything to do with the fact that i'm starting the animation is the `viewDidLayoutSubviews`? – N. Chalifour Nov 25 '17 at 16:52