2014-10-07 7 views
6

Come posso aspettare che un'animazione finisca prima che inizi il prossimo in Swift? Ho fatto casino con if animation.animationDidStop... {}, ma non funzionerà.Come posso terminare un'animazione prima che inizi il prossimo in Swift?

Ecco alcuni del mio codice finora:

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

Utilizzando classe 'func animateWithDuration (_ durata: NSTimeInterval, animazioni animazioni:() -> Void, il completamento esecuzione:? ((Bool) -> Void))' e chiamando la seconda animazione sul completamento ? – Larme

+0

e in che modo esattamente dovrei implementarlo nel mio codice? :) – user3157462

+0

Mi spiace, ho letto male, stai usando animationImages. Ti suggerisco di utilizzare l'equivalente in Swift: http://stackoverflow.com/questions/9283270/access-method-after-uiimageview-animation-finish – Larme

risposta

1

Ehm, probabilmente risposto prima, ma è possibile utilizzare Grand Central Dispatch dispatc_aysnc.

L'idea è che, si conosce la durata dell'animazione, quindi lo si usa per indicare al GDC quando eseguire il codice successivo. Quindi, qualcosa di simile a:

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

Per un'animazione dopo l'altro è possibile utilizzare NSTimer così ...

Vedere il mio codice, potrebbe aiutare ...

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

} 

Spiegazione. ..

  • Qui startAnimation1() metodo sta chiamando a viewDidLoad()
  • Poi sto chiamando il metodo di NSTimer per startAnimation2(), Notare che è startAnimation2() il metodo viene chiamato dopo 2 secondi ..., vale a dire Dopo la prima animation finito ...

Grazie

Problemi correlati