user2722667
user2722667

Reputation: 8651

Swift ios cant get animation to be infinite

I have a animation that I want to be infinite

var rotateRock: Bool = true
    func rotateRock() {
            UIView.animate(withDuration: 2.7, delay: 2.7, options: [UIViewAnimationOptions.repeat, UIViewAnimationOptions.curveLinear], animations:
                {
                    self.rockImg.transform.ty += 155
                    if self.rotateRock {
                    self.rotateRock = false
                    self.rockImg.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat(M_PI)) / 180.0)
                } else {
                    self.rotateRock = true
                    self.rockImg.transform = CGAffineTransform(rotationAngle: 0)
                }

            },completion: nil)
        }

Then I call rotateRock() inside viewDidLoad() this works, the animation never stops which is what I want.

If I now enter another VC that is presented modally then exit it so I end up in my first VC the animation is still running which I want.

But if I change VC by clicking on another tab in my tab bar and then go back the animation has stopped. Is there any way to check if the animation has stopped so that I can restart it?

Upvotes: 0

Views: 383

Answers (1)

You could override viewDidAppear() and call rotateRock there. That should work.

Edit: What Matt says below is true, viewDidAppear would work but maybe it would be better viewWillAppear to make sure you don't see it before starting.

Upvotes: 1

Related Questions