John
John

Reputation: 1

Stop animation of first view controller when second is started swift programming

How to stop animation of first view controller when second is started in swift programming. I have created a function which stops animation in first view controller. I want it to be called in second view controller.

In first View Controller

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

Not sure how to call this function in second view controller.

Upvotes: 0

Views: 112

Answers (1)

You can create a delegate something like:

protocol StopAnimationDelegate{
    func stopAnimations()
}

Then, on your first view controller you're going to adopt this protocol:

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

On your second view controller, you can make something like:

class SecondViewController: UIViewController{
    var delegate:StopAnimationDelegate?

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

Note: That's a way of how you can accomplish that, but all depends on what you need to do, for example you can simply stop the animations when you perform the segue (but again, that depends on what you want to do).

Another option, is using NSNotificationCenter to post a notification to Stop the animation, something like:

In First View Controller:

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

}

Upvotes: 1

Related Questions