¿Cómo espero a que termine una animación antes de que comience la siguiente en Swift?

¿Cómo espero a que termine una animación antes de que comience la siguiente en Swift? he estado jugando con if animation.animationDidStop... {}, pero no funcionará.

Aquí hay algo de mi código hasta ahora:

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

preguntado Oct 07 '14, 14:10

Usar class func animateWithDuration(_ duration: NSTimeInterval, animations animations: () -> Void, completion completion: ((Bool) -> Void)?) y llamando a la segunda animación al finalizar? -

y ¿cómo exactamente implementaría esto en mi código? :) -

Lo siento, leí mal, estás usando animationImages. Te sugiero que uses el equivalente en Swift: stackoverflow.com/questions/9283270/… -

2 Respuestas

Erm, probablemente respondido antes, pero puede usar Grand Central Dispatch dispatc_aysnc.

La idea es que conozca la duración de la animación, por lo que la usa para decirle al GDC cuándo ejecutar el siguiente código. Entonces algo como:

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

});

Respondido 07 Oct 14, 14:10

Para una animación tras otra puedes usar NSTemporizador también...

Mira mi código, podría ayudar...

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

}

Explicación...

  • Aquí iniciarAnimación1() el método está llamando viewDidLoad ()
  • Entonces estoy llamando al método de NSTimer para iniciarAnimación2(), Aquí tenga en cuenta que iniciarAnimación2() el método se llama después de 2 segundos..., es decir, después de que finaliza la primera animación...

Muchas Gracias

Respondido el 05 de diciembre de 14 a las 11:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.