第二个启动时停止第一个视图控制器的动画快速编程

时间:2016-06-21 20:48:16

标签: swift swift2

如何在swift编程中启动秒时停止第一个视图控制器的动画。我创建了一个在第一个视图控制器中停止动画的功能。我想在第二个视图控制器中调用它。

在第一个视图控制器

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

不确定如何在第二个视图控制器中调用此函数。

1 个答案:

答案 0 :(得分:1)

您可以创建类似的代理:

class testController(http.Controller):
       { i want to get name of my model ( my.model) here }

然后,在您的第一个视图控制器上,您将采用此协议:

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

注意:这是一种如何实现这一目标的方法,但一切都取决于你需要做什么,例如你可以在执行segue时简单地停止动画(但同样,这取决于你的内容)想做)。

另一个选项,正在使用NSNotificationCenter发布停止动画的通知,例如:

在First View Controller中:

class SecondViewController: UIViewController{
    var delegate:StopAnimationDelegate?

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