2016-05-25 7 views
0

ユーザーが戻るボタンをタップしてルートビューコントローラに戻るときに、アニメーションを実行したいと思います。アニメーションは、ユーザが詳細ビューコントローラで行った変更を単に強調表示します。カスタムアニメーションが完了するまで、viewWillDisappearの実行を待機しますか?

私はこれを試しました。アニメーション自体は機能しています(私の質問には本質的なことではありません。私がやっていることを説明するために残しておきます)。問題は、セグリングが速すぎてアニメーションを見ることができないことです。

アニメーションが完了するまで、どのように私はwaitのviewWillDisappearの実行でできますか?

override func viewWillDisappear(animated: Bool) { 
     // ... 

     // Animate if text changes. reminderAfterRulesRun is a custom data structure. reminderNameTextInput is my outlet to my label 
     if reminderNameTextInput.text != reminderAfterRulesRun.title { 

      let originalreminderNameTextInputColor = self.reminderNameTextInput.textColor 

      // Animate the removing of "All" and replacing it with the most commonly used list. 
      UIView.animateWithDuration(0.3, delay: 0, options: .Autoreverse, animations: { 

       // Fade out 
       self.reminderNameTextInput.textColor = UIColor.redColor() 
       self.reminderNameTextInput.text = reminderAfterRulesRun.title 
       self.reminderNameTextInput.alpha = 0.0 

       }, completion: { 
        (finished: Bool) -> Void in 

        // Once the label is completely invisible, set the text and fade it back in 
        UIView.animateWithDuration(0.3, delay: 0, options: .Autoreverse, animations: { 
         //      self.reminderNameTextInput.selectedSegmentIndex = self.toSegmentedControlValue(reminderAfterRulesRun.reminderNameTextInput)! 
         self.reminderNameTextInput.text = reminderAfterRulesRun.title 
         self.reminderNameTextInput.textColor = originalreminderNameTextInputColor 
         self.reminderNameTextInput.alpha = 1.0 
         }, completion: nil)   
      }) 
     } 
} 
+0

私はviewWillDisappearがこれを行うには、適切な場所であるとは思わない:このプロトコルは、移行

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext { return 0.25; } 

してから移行を実行するための呼び出しの時間を求める方法があります。私はあなたがアニメーションのための別の機能を構築することを検討し、あなたがしたいときに欲しいと思うと思う。(アニメーションが終了した後) –

+0

@ウィリアムGPしかし、ユーザーがルートビューコントローラーに戻るときにアニメーションを起こしたい。だから、どうしたらいいの? – Daniel

+0

現在あなたのセグアはどこですか?どのようにトリガーされますか? –

答えて

1

View Controller Transition APIを使用するように見えます。高いレベルでは、View ControllerにUINavigationControllerDelegateプロトコルメソッドを実装する必要があります。あなたのNav ControllerのデリゲートとしてView Controllerを設定し、移行が起きると、このメソッドが呼び出されます。ここでは、相互作用にどのView Controllerが関与しているか、および遷移がどの方向に進んでいるか(PushまたはPop)を確認して確認できます。この情報を使って、適切なアニメーターを提供することができます。

- (id<UIViewControllerAnimatedTransitioning>) 
       navigationController:(UINavigationController *)navigationController 
    animationControllerForOperation:(UINavigationControllerOperation)operation 
       fromViewController:(UIViewController*)fromVC 
        toViewController:(UIViewController*)toVC 
{ 
if (operation == UINavigationControllerOperationPush) { 
    return self.animator; 
} 
    return nil; 
} 

"アニメータは" UIViewControllerAnimatedTransitioningプロトコルを実装NSObjectのサブクラスです。

- (void)animateTransition: (id<UIViewControllerContextTransitioning>)transitionContext 
{ 
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; 
UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; 
[[transitionContext containerView] addSubview:toViewController.view]; 
toViewController.view.alpha = 0; 

[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ 
    fromViewController.view.transform = CGAffineTransformMakeScale(0.1, 0.1); 
    toViewController.view.alpha = 1; 
} completion:^(BOOL finished) { 
    fromViewController.view.transform = CGAffineTransformIdentity; 
    [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; 

}]; 

} 
+0

ありがとう、私はこれを試してみます。しかし、これが私の必要としていることを確信していますか?テキストフィールドでアニメーションを実行してから、ルートビューコントローラーに戻るようにしたいと思います。ですから、私はセグをするときにトランジション自体をアニメートしようとしていません。ビューの前にテキストフィールドのカスタムアニメーションだけが消えます。 – Daniel

+0

私はあなたがこのアプローチでそれを達成できると信じています。アニメーション中にViewControllerの位置を完全に制御できるようになるまで、遷移時間を制御してゆくことができます。ちょっとした作業で、すべてのアニメーションが完了するまでView Controllerの位置を固定することができます。それらが完了すると、View Controllerをオフスクリーンでアニメートし、新しいView Controllerをアニメートします。おかげさまで –

+0

あなたはこのためのスウィフトコードを持っていますか?私は、アニメーションの完成までviewWillDisappearの実行を停止するいくつかの種類の「待機」メソッドがあれば簡単な解決策があると思っていました。 – Daniel

関連する問題