0

当用户点击后退按钮以返回我的根视图控制器时,我想执行动画。动画将简单地突出显示用户在详细视图控制器中所做的更改。

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

在动画完成之前,如何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)         
            })
        }
}
4

1 回答 1

1

看起来你会想要使用 View Controller Transition API。在高层次上,您需要UINavigationControllerDelegate在 View Controller 中实现协议方法。当您将 View Controller 设置为 Nav 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 子类。该协议具有询问转换时间的方法

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

然后呼吁实施过渡:

- (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]];

}];

}
于 2016-05-25T03:07:35.567 回答