2014-10-07 11 views
6

Jak mogę poczekać na zakończenie jednej animacji, zanim kolejna rozpocznie się w Swift? Rozmyślałem z if animation.animationDidStop... {}, ale to nie zadziała.Jak mogę poczekać na zakończenie jednej animacji, zanim kolejna rozpocznie się w Swift?

Oto niektóre z moich kodu do tej pory:

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

Korzystanie 'animateWithDuration klasa funkcjono (_ Czas trwania: NSTimeInterval, animacje animacje() -> Pustki, zakończenie realizacji: ((Bool) -> void)) i' wywołanie drugiej animacji po zakończeniu ? – Larme

+0

i jak dokładnie zaimplementowałem to w moim kodzie? :) – user3157462

+0

Przepraszam, źle odczytałem, używasz animacjiImages. Proponuję użyć odpowiednika w Swift: http://stackoverflow.com/questions/9283270/access-method-after-uiimageview-animation-finish – Larme

Odpowiedz

1

Erm, prawdopodobnie odpowiedział wcześniej, ale można użyć Grand Central Dispatch dispatc_aysnc.

Chodzi o to, że znasz czas trwania animacji, więc użyjesz go, aby poinformować GDC, kiedy należy wykonać następny kod. Więc coś takiego:

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

animacji dla jednego po drugim można użyć NSTimer, jak również ...

Zobacz mój kod, to może pomóc ...

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

} 

Wyjaśnienie. ..

  • Tutaj StartAnimation1() metoda jest wywołanie w viewDidLoad()
  • Potem dzwonię metodę NSTimer za startAnimation2(), Tutaj pamiętać, że startAnimation2() metoda nazywa się po 2 sekundach ... czyli po raz pierwszy animacja wykończone ...

Dzięki

Powiązane problemy