但是,有时候,我会在结尾再加点一些令人分心的动画(似乎重复动画的后半部分).使用相当简单的动画,我很少在iPhone 5上看到这种症状(虽然我在慢速笔记本电脑上工作时经常在模拟器上看到它).如果我使动画在计算上更加昂贵(例如大量的阴影,多个视图动画化不同的方向等),则设备上的这个问题的频率增加.
有没有人看到这个问题,并找出一个解决方案,而不是简化动画(我承认应该做的)和/或写我自己的交互控制器? UIPercentDrivenInteractiveTransition方法具有一定的优雅性,但是我对不确定性的行为感到不安.有人看到这个行为吗?有没有人知道其他解决方案?
为了说明效果,请参见下图.注意第二场景,红色视图,动画结束时,似乎第二次重复其动画的后半部分.
此动画由以下生成:
>反复调用updateInteractiveTransition,进度从0%更新到40%;
>暂时暂停(所以你可以区分交互过渡和finishInteractiveTransition产生的完成动画);
>然后调用finishInteractiveTransition来完成动画;和
>动画控制器的动画完成块调用completeTransition的transitionContext,以便清理所有东西.
做一些诊断,似乎是最后一步触发动画的无关的一点.动画控制器的完成块在动画完成时被调用,但一旦调用completeTransition,它有时会重复动画的最后一位(特别是使用复杂动画时).
我不认为它是相关的,但这是我的配置导航控制器执行交互过渡的代码:
- (void)viewDidLoad { [super viewDidLoad]; self.navigationController.delegate = self; self.interationController = [[UIPercentDrivenInteractiveTransition alloc] init]; } - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController*)fromVC toViewController:(UIViewController*)toVC { if (operation == UINavigationControllerOperationPush) return [[PushAnimator alloc] init]; return nil; } - (id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController*)navigationController interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>)animationController { return self.interationController; }
我的PushAnimator是:
@implementation PushAnimator - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext { return 5.0; } - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext { UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; [[transitionContext containerView] addSubview:toViewController.view]; toViewController.view.frame = CGRectOffset(fromViewController.view.frame,fromViewController.view.frame.size.width,0);; [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ toViewController.view.frame = fromViewController.view.frame; } completion:^(BOOL finished) { [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; }]; } @end
注意,当我将logging语句放在我称为completeTransition的位置时,我可以看到,在调用completeTransition之后,动画的这个无关的位置就会发生(即使动画在那一刻真的完成了).这将表明额外的动画可能是调用completeTransition的结果.
我已经用手势识别器完成了这个实验:
- (void)handlePan:(UIScreenEdgePanGestureRecognizer *)gesture { CGFloat width = gesture.view.frame.size.width; if (gesture.state == UIGestureRecognizerStateBegan) { [self performSegueWithIdentifier:@"pushToSecond" sender:self]; } else if (gesture.state == UIGestureRecognizerStateChanged) { CGPoint translation = [gesture translationInView:gesture.view]; [self.interactionController updateInteractiveTransition:ABS(translation.x / width)]; } else if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled) { CGPoint translation = [gesture translationInView:gesture.view]; CGPoint velocity = [gesture velocityInView:gesture.view]; CGFloat percent = ABS(translation.x + velocity.x * 0.25 / width); if (percent < 0.5 || gesture.state == UIGestureRecognizerStateCancelled) { [self.interactionController cancelInteractiveTransition]; } else { [self.interactionController finishInteractiveTransition]; } } }
我也是通过手动调用updateInteractiveTransition和finishInteractiveTransition(从方程式中消除手势识别器)来完成的,它仍然表现出这种奇怪的行为:
[self performSegueWithIdentifier:@"pushToSecond" sender:self]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0 * NSEC_PER_SEC)),dispatch_get_main_queue(),^{ [self.interactionController updateInteractiveTransition:0.40]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW,^{ [self.interactionController finishInteractiveTransition]; }); });
底线,我得出结论,这是一个孤立到UIPercentDrivenInteractiveTransition与复杂动画的问题.我可以通过简化它们来减少问题(例如快照和动画快照视图).我也怀疑我可以通过不使用UIPercentDrivenInteractiveTransition来解决这个问题,并且编写自己的交互控制器,这样做就可以做动画本身,而不用插入animationWithDuration块.
但是我想知道是否有人想出了使用复杂动画使用UIPercentDrivenInteractiveTransition的其他技巧.
解决方法
} completion:^(BOOL finished) { double delayInSeconds = 0.1; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW,(int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime,^(void){ BOOL cancelled = [transitionContext transitionWasCancelled]; [transitionContext completeTransition:!cancelled]; }); self.interacting = NO; }];
另一种可能性是:不要使用百分比驱动动画!我手动开动互动式自定义动画时,我从未遇到过这样的问题.