【问题标题】:How do I wait for one animation to finish before the next one starts in Swift?如何在 Swift 中开始下一个动画之前等待一个动画完成?
【发布时间】:2014-12-01 22:25:20
【问题描述】:

如何在 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()
}

【问题讨论】:

  • 使用class func animateWithDuration(_ duration: NSTimeInterval, animations animations: () -> Void, completion completion: ((Bool) -> Void)?)并在完成时调用第二个动画?
  • 我将如何在我的代码中实现这一点? :)
  • 抱歉,我看错了,您使用的是动画图像。我建议你在 Swift 中使用等价的:stackoverflow.com/questions/9283270/…

标签: ios xcode swift uianimation


【解决方案1】:

嗯,之前可能已经回答过了,但你可以使用 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);

});

【讨论】:

    【解决方案2】:

    对于一个接一个的动画,您也可以使用 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() 中调用
    • 然后我为 startAnimation2() 调用 NSTimer 的方法, 这里注意 startAnimation2() 方法在 2 秒后被调用..., 即第一个动画完成后...

    谢谢

    【讨论】:

      猜你喜欢
      • 2014-01-24
      • 1970-01-01
      • 2016-11-26
      • 2016-11-29
      • 2021-05-25
      • 1970-01-01
      • 2012-12-30
      • 2019-07-26
      • 2023-03-17
      相关资源
      最近更新 更多