等待viewWillDisappear的执行,直到自定义动画完成?

时间:2016-05-25 02:53:40

标签: ios swift animation wait viewwilldisappear

当用户点击后退按钮以切换回我的Root View Controller时,我想要执行动画。动画将仅突出显示用户在详细视图控制器中所做的更改。

我试过这个。动画本身是有效的(对我的问题并不是很重要,只是把它留下来说明我在做什么。)问题是segueing发生得太快而且你看不到动画。

在动画完成之前,我如何wait执行viewWillDis吗?

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

1 个答案:

答案 0 :(得分:1)

您似乎想要使用View Controller Transition API。在较高级别,您需要在View Controller中实现-trustcacerts协议方法。将View Controller设置为Nav Controller的委托并且即将发生转换时,将调用此方法。在这里,您可以查看和查看视图控制器参与交互以及转换的方向(推送或弹出)。有了这些信息,您可以提供适当的动画师。

UINavigationControllerDelegate

&#34;动画师&#34;是一个实现- (id<UIViewControllerAnimatedTransitioning>) navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController*)fromVC toViewController:(UIViewController*)toVC { if (operation == UINavigationControllerOperationPush) { return self.animator; } return nil; } 协议的NSObject子类。该协议具有询问转换时间的方法

UIViewControllerAnimatedTransitioning

然后调用实现转换:

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