Подождите с выполнением viewWillDisappear, пока пользовательская анимация не завершится?
Я хотел бы выполнить анимацию, когда пользователь нажимает кнопку "Назад", чтобы перейти к моему Root View Controller. Анимация просто выделит изменения, внесенные пользователем в детализированном контроллере.
Я попробовал это. Сама анимация работает (и не очень важна для моего вопроса, просто оставьте ее, чтобы проиллюстрировать, что я делаю.) Проблема в том, что передача происходит слишком быстро, и вы не видите анимацию.
Как я могу 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)
})
}
}
1 ответ
Похоже, вы захотите использовать API перехода View Controller. На высоком уровне вам нужно будет реализовать UINavigationControllerDelegate
Методы протокола в вашем View Controller. Когда вы устанавливаете ваш View Controller в качестве делегата вашего Nav 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;
}
"Аниматор" - это подкласс NSObject, который реализует UIViewControllerAnimatedTransitioning
протокол. В этом протоколе есть методы, которые запрашивают время перехода
- (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]];
}];
}