2016-06-21 4 views
0

即時プログラミングで秒を開始すると、第1ビューコントローラのアニメーションを停止する方法。最初のビューコントローラでアニメーションを停止する関数を作成しました。私はそれを2番目のView Controllerで呼びたいと思っています。最初のビューコントローラで第2回目の起動時に第1ビューコントローラのアニメーションを停止します。

func stopAni(){ 
     self.resultView.stopAnimating() 
     ButtonAudioPlayer.stop() 
     ButtonAudioPlayer1.stop() 
     ButtonAudioPlayer2.stop() 
     ButtonAudioPlayer3.stop() 
     ButtonAudioPlayer4.stop() 
     ButtonAudioPlayer5.stop() 
     ButtonAudioPlayer6.stop() 

第2のビューコントローラでこの関数を呼び出す方法がわかりません。することができます、あなたの第2のビューコントローラで

class FirstViewController : UIViewController, StopAnimationDelegate{ 
    //..... here code 

    func stopAnimations(){ 
     //Stop your animations or call your method stopAni here. 
    } 

    //.... here more code 

    @IBAction func openSecondViewController(sender:UIButton){ 
     self.performSegueWithIdentifier("segue_first_second",sender:nil) 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "segue_first_second"{ 
      let secondViewController = segue.destinationViewController as! SecondViewController 
      secondViewController.delegate = self 
     } 
    } 
} 

protocol StopAnimationDelegate{ 
    func stopAnimations() 
} 

次に、あなたの最初のビューコントローラ上で、あなたがこのプロトコルを採用するつもりだ:

答えて

1

あなたのようなデリゲートのものを作成することができます

class SecondViewController: UIViewController{ 
    var delegate:StopAnimationDelegate? 

    @override func viewDidLoad(){ 
     delegate?.stopAnimations() 
    } 
} 

注:それはあなたがそれを達成する方法ですが、すべてのdeあなたがしなければならないことに依存しています。たとえば、セグを実行したときにアニメーションを停止することができます(ただし、やりたいことに依存します)。

別のオプションは、アニメーションを停止する通知を投稿するNSNotificationCenterを使用して、何かのように:最初のビューコントローラで

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "stopAnim", name: "kStopAnimations", object: nil) 
    } 

    //...Your stopAnim method 

    //... More Code 

} 

class SecondViewController : UIViewController{ 

    override func viewDidLoad() { 
     NSNotificationCenter.defaultCenter().postNotificationName("kStopAnimations", object: nil) 
    } 

} 
+0

私はあなたが提案しSWIFTコードを使用するが、それはありますうまくいきません! @JoséRoberto Abreu – John

+0

@Johnサンプルプロジェクトをアップロードして確認できますか? –

関連する問題