2016-06-21 20 views
0

如何在快速編程中啓動第二個視圖控制器時停止第一個視圖控制器的動畫。我創建了一個在第一個視圖控制器中停止動畫的函數。我希望它在第二個視圖控制器中被調用。快速編程時停止第一個視圖控制器的動畫快速編程

在第一視圖控制器

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

不知道如何調用在第二視圖控制器此功能。

回答

1

您可以創建一個委託是這樣的:

protocol StopAnimationDelegate{ 
    func stopAnimations() 
} 

那麼,你的第一個視圖控制器上你要採取這個協議:

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

關於第二個視圖控制器,你可以做類似:

class SecondViewController: UIViewController{ 
    var delegate:StopAnimationDelegate? 

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

注:這是一個如何實現這一目標的一種方式,但所有德依賴於你需要做的事情,例如你可以簡單地停止動畫,當你執行segue(但是,這又取決於你想要做什麼)。

另一個選項,使用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你可以上傳一個示例項目來檢查嗎? –