簡體   English   中英

如何在Swift中下一個動畫開始之前等待一個動畫完成?

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

如何在Swift中下一個動畫開始之前等待一個動畫完成? 我一直在搞亂if animation.animationDidStop... {} ,但它不會起作用。

這是我目前的一些代碼:

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()
}

嗯,可能之前回答過,但你可以使用Grand Central Dispatch dispatc_aysnc。

我們的想法是,您知道動畫持續時間,因此您可以使用它來告訴GDC何時執行下一個代碼。 所以類似於:

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

});

對於一個又一個動畫,您也可以使用NSTimer ......

看到我的代碼,它可能會有所幫助......

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.
}

}

說明...

  • 這里startAnimation1()方法在viewDidLoad()中調用
  • 然后我打電話的NSTimer對startAnimation2()方法,在這里指出,startAnimation2()方法被調用...,即在首先結束的動畫2秒后...

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM