Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I wait for one animation to finish before the next one starts in Swift?

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()
}
like image 368
user3157462 Avatar asked Oct 07 '14 13:10

user3157462


People also ask

What is Core Animation in Swift?

Overview. Core Animation provides high frame rates and smooth animations without burdening the CPU and slowing down your app. Most of the work required to draw each frame of an animation is done for you.

How do I show an animation label in Swift?

width will make it appear at the left of the view, so you can't see any effect on it. This will make your label appear from the left to the right. This is the correct way programmatically, but the animateWithDuration should be placed in viewDidAppear to achieve the desired effect.


1 Answers

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);

});
like image 116
Zhang Avatar answered Sep 30 '22 19:09

Zhang