6

How do I wait for one animation to finish before the next one starts in Swift? I have been messing around with if animation.animationDidStop... {}, but it won't work.

Here's some of my code so far:

class ViewController: UIViewController {
@IBOutlet weak var purpleRing: UIImageView!
@IBOutlet weak var beforeCountdownAnimation: UIImageView!

var imageArray = [UIImage]()
var imageArray2 = [UIImage]()

override func viewDidLoad() {
    super.viewDidLoad()

    for e in -17...0 {
    let imageName2 = "\(e)"
        imageArray2.append(UIImage(named: imageName2)!)
    }

    for t in 1...97 {
        let imageName = "\(t)"
        imageArray.append(UIImage(named: imageName)!)
    }
}

func startAnimation() -> Void {
    purpleRing.animationImages = imageArray
    purpleRing.animationDuration = 5.0
    purpleRing.startAnimating()
}

func startAnimation2() -> Void {
    beforeCountdownAnimation.animationImages = imageArray2
    beforeCountdownAnimation.animationDuration = 1.0
    beforeCountdownAnimation.startAnimating()
}

@IBAction func startAnimations(sender: AnyObject) {
    startAnimation()
    startAnimation2()
}
3
  • Using class func animateWithDuration(_ duration: NSTimeInterval, animations animations: () -> Void, completion completion: ((Bool) -> Void)?) and calling the second animation on the completion?
    – Larme
    Oct 7, 2014 at 13:29
  • and how exactly would I implement this in my code? :) Oct 7, 2014 at 13:32
  • Sorry, I misread, you're using animationImages. I'd suggest you use the equivalent in Swift: stackoverflow.com/questions/9283270/…
    – Larme
    Oct 7, 2014 at 13:34

2 Answers 2

1

Erm, probably answered before, but you can use Grand Central Dispatch dispatc_aysnc.

The idea is that, you know the animation duration, so you use that to tell the GDC when to execute the next code. So something like:

// call animation 1, which you specified to have 5 second duration
CGFloat animation1Duration = 5.0;
CGFloat animation2Duration = 7.0;

playAnimation1WithDuration(animation1Duration);

// calling second animation block of code after 5.0 using GDC
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(animation1Duration * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), { () -> Void in

    print("5.0 has passed");

    // call second animation block here
    playAnimation2WithDuration(animation2Duration);

});
0

For one animation after another you can use NSTimer as well...

See my code,it might help...

class ViewController: UIViewController {

@IBOutlet weak var IBimg1: UIImageView!
@IBOutlet weak var IBimg2: UIImageView!
@IBOutlet weak var IBimg3: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    startAnimation1()

}

func startAnimation1(){

NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: Selector("startAnimation2"), userInfo: self, repeats: false)

UIView.animateWithDuration(2.0, animations: {

        self.IBimg1.alpha = 0

    })

}

func startAnimation2(){

NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: Selector("startAnimation3"), userInfo: self, repeats: false)

    UIView.animateWithDuration(2.0, animations: {

        self.IBimg2.alpha = 0

    })

}

func startAnimation3(){

    UIView.animateWithDuration(2.0, animations: {

        self.IBimg3.alpha = 0

    })

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

Explanation...

  • Here startAnimation1() method is calling in viewDidLoad()
  • Then i'm calling NSTimer's method for startAnimation2(), Here note that startAnimation2() method is called after 2 seconds..., i.e After first animation finished...

Thanks

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.