2016-06-21 3 views
0

빠른 프로그래밍에서 초를 시작할 때 첫 번째보기 컨트롤러의 애니메이션을 중지하는 방법. 첫 번째보기 컨트롤러에서 애니메이션을 중지하는 함수를 만들었습니다. 두 번째 뷰 컨트롤러에서 호출되도록하고 싶습니다. 첫 번째보기 컨트롤러에서초를 시작할 때 첫 번째보기 컨트롤러의 애니메이션 중지 빠른 프로그래밍

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

초 뷰 컨트롤러에서이 함수를 호출하는 방법을 잘하지 않습니다. 당신이 할 수있는, 두 번째 뷰 컨트롤러에서

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

참고 : 어떻게 달성 할 수 있겠습니까? 당신이해야 할 일을 고수합니다. 예를 들어, 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

난 당신에 의해 제안 신속한 코드를 사용하지만입니다 작동 안함! @ José Roberto Abreu – John

+0

@John 샘플 프로젝트를 업로드하여 업로드 할 수 있습니까? –