该视频显示了我遇到的问题。 http://www.youtube.com/watch?v=C9od_2KZAbs
我正在尝试使用 UIPanGestureRecognizer 创建自定义推送交互式转换。我有一个交互式过渡委托(使用 UIPercentDrivenInteractiveTransition)和一个过渡动画师。
以下是平移手势控制过渡的方式:
- (void) panGestureRecognized:(UIPanGestureRecognizer *) gestureRecogznier {
CGPoint translation = [gestureRecogznier translationInView:gestureRecogznier.view];
if (gestureRecogznier.state == UIGestureRecognizerStateBegan) {
self.interactiveTransitionAnimator = [[UIPercentDrivenInteractiveTransition alloc] init];
EVDetailViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"EVDetailViewController"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
else if (gestureRecogznier.state == UIGestureRecognizerStateChanged) {
CGFloat d = (translation.x / CGRectGetWidth(self.view.bounds)) * -1;
[self.interactiveTransitionAnimator updateInteractiveTransition:d];
}
else if (gestureRecogznier.state == UIGestureRecognizerStateEnded) {
if ([gestureRecogznier velocityInView:self.view].x < 0) {
[self.interactiveTransitionAnimator finishInteractiveTransition];
} else {
[self.interactiveTransitionAnimator cancelInteractiveTransition];
}
self.interactiveTransitionAnimator = nil;
}
}
UINavigationControllerDelegate 处理两个转换委托对象的出售,这在调用 pushViewController: 时触发。
过渡动画师有一个非常简单的动画:
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
[[transitionContext containerView] addSubview:toViewController.view];
CGRect finalToVCFrame = [transitionContext finalFrameForViewController:toViewController];
if (self.operation == UINavigationControllerOperationPush) {
// set offscreen to the right
toViewController.view.frame = CGRectMake(320.0f, 0.0f, 320.0f, 568.0f);
[UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0f options:0 animations:^{
toViewController.view.frame = finalToVCFrame;
} completion:^(BOOL finished) {
// *** When this is called, the glitch occurs
[transitionContext completeTransition:YES];
}];
}
}
在视频中,平移手势在大约 60% 处结束,这会触发 finishInteractiveTransition 调用。一切顺利,直到 UIView 基于块的动画中的完成块调用 completeTransition:。调用此方法时,toViewController 会不必要地重新设置推送动画的最后一部分。在视频中,红色视图是窗口。动画持续时间为 3 秒。
我无法弄清楚为什么会这样。看起来手势结束和完成转换:被调用之间的动画发生了两次。有任何想法吗?