2017-01-25 2 views
1

私は比較的プログラミングが新しく、現在3つ(可能な場合はそれ以上)のビューコントローラを使ってアプリケーションを構築しようとしています。私はチュートリアル(https://www.youtube.com/watch?v=B9sH_VxPPo4&t=505s)を行って、カスタムボタンを押した後に2つのView Controllerの間でアニメーションを学んだ。これはすべて完璧に機能しました。SWIFT 3のボタンで3つ以上のビューコントローラ間のトランジションをアニメーション化する方法は?

しかし、私は別のビューコントローラ(ThirdViewController)と2番目のボタン(showThirdVCButton)を実装しようとしています。

トランジションは両方のボタンに対して完全に機能しますが、アニメーションは標準のアニメーションに戻されます。

これは最初のビューコントローラでは私のコードです:

import UIKit 

class ViewController: UIViewController, UIViewControllerTransitioningDelegate { 



@IBOutlet weak var showSecondVCButton: UIButton! 
@IBOutlet weak var showThirdVCButton: UIButton! 


let transition = CircularTransition() 

override func viewDidLoad() { 
    super.viewDidLoad() 


    //I customise my buttons here 
    showSecondVCButton.layer.cornerRadius = showSecondVCButton.frame.size.width/2 
    showThirdVCButton.layer.cornerRadius = showSecondVCButton.frame.size.width/2 
} 

//The destination for each button is declared here and the animation style 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "secondVCSegue" { 
     let secondVC = segue.destination as! SecondViewController 
     secondVC.transitioningDelegate = self 
     secondVC.modalPresentationStyle = .custom 
    } 

    if segue.identifier == "thirdVCSegue" { 
     let thirdVC = segue.destination as! ThirdViewController 
     thirdVC.transitioningDelegate = self 
     thirdVC.modalPresentationStyle = .custom 
    } 

} 


func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { 

    transition.transitionMode = .present 

    if showSecondVCButton.isTouchInside == true { 
     transition.startingPoint = showSecondVCButton.center 
     transition.circleColor = showSecondVCButton.backgroundColor! 
    } 

    if showThirdVCButton.isTouchInside == true { 

     transition.startingPoint = showThirdVCButton.center 
     transition.circleColor = showThirdVCButton.backgroundColor! 
    } 

    return transition 
} 


func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
    transition.transitionMode = .dismiss 

    if showSecondVCButton.isTouchInside == true { 
     transition.startingPoint = showSecondVCButton.center 
     transition.circleColor = showSecondVCButton.backgroundColor! 
    } 

    if showThirdVCButton.isTouchInside == true { 

     transition.startingPoint = showThirdVCButton.center 
     transition.circleColor = showThirdVCButton.backgroundColor! 
    } 

    return transition 
} 


override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

}

はあなたの助けをありがとう!

+0

あなたが追加しました'if segue.identifier ==" thirdVCSegue "{...}'コード内のブレークポイントにアクセスしていることを確認しますか? segueの識別子にエラーがあると、コードが実行されなくなる可能性があります。 – Rob

答えて

1

huberdo - 私はあなたが見ていたのと同じチュートリアルに従いました。最初から作成するのではなく、2番目のボタンをコピーしたときに得たエラーを再現することができました。見て、各ボタンのための1つだけのセグを持っていることを確認してください。

私はあなたがコードを簡素化できると思います。私はいつもボタンのアクションを設定して、あなたのコード内でどこに呼び出されているのかを見ることができます。今はストーリーボードの設定と明示的なコードの組み合わせです。

あなたはボタンのアクションを使用して、どのボタンを追跡するフラグを設定した場合は押されて、それはすべてがずっと簡単になり

var transition = CircularTransition() 
var startingView = UIView() // this will define the starting point for all transitions 

// 
// 
// explicit actions for the buttons 
@IBAction func cmdOneAction(_ sender: Any) 
{ 
    startingView = sender as! UIView 
    self.performSegue(withIdentifier: "segueShow1", sender: self) 
} 

@IBAction func cmdTwoAction(_ sender: Any) 
{ 
    startingView = sender as! UIView 
    self.performSegue(withIdentifier: "segueShow2", sender: self) 
} 

簡素化されたアニメーション機能

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { 

    transition.transitionMode = .present 
    transition.startingPoint = startingView.center 
    transition.circleColor  = startingView.backgroundColor! 

    return transition 
} 


func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { 

    transition.transitionMode = .dismiss   
    transition.startingPoint = startingView.center 
    transition.circleColor  = startingView.backgroundColor! 

    return transition 
} 
+0

ありがとうございました。私は提案したコードにコードを採用しましたが、アニメーションはまだ基本に設定されています。トランスミッションは正常に動作します。私の '' segue.identifier == "" {'?おそらく – huberdo

+0

segueの識別子と一致しない場合は、 ""をチェックしている場合は表示されませんので、常にデフォルトのバージョンを実行します – Russell

+0

ありがとうRussel!いいね、あなたのような人がいます:)素敵な一日を! – huberdo

関連する問題