2

我尝试使用新的 iOS7 API 实现交互式自定义模式 UIViewController 转换。只要我不使用新的,一切都按预期工作

 + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

动画过渡的方法。如果我通过按钮调用转换,它工作正常。当过渡是交互式的,由 GestureRecognzier 驱动时,就会出现问题。如果我松开手指,动画会立即被切断并且不会完成。如果我使用相同的代码正在工作

animateWithDuration:delay:options:animations:completion:

反而。

这是动画转换的代码:

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
   UIViewController *fromViewController = [transitionContext  viewControllerForKey:UITransitionContextFromViewControllerKey];
   UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
   CGRect fullFrame = transitionContext.containerView.frame;

if (!self.dismissal) {
    [transitionContext.containerView addSubview:fromViewController.view];
    [transitionContext.containerView addSubview:toViewController.view];
    fromViewController.view.frame = fullFrame;
    toViewController.view.frame = CGRectMake(0, fullFrame.size.height, 0, 0);

    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionTransitionNone animations:^{
        //animation
        toViewController.view.frame = CGRectMake(0,0, fullFrame.size.width, fullFrame.size.height);
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];

} else {
    [transitionContext.containerView addSubview:toViewController.view];
    [transitionContext.containerView addSubview:fromViewController.view];
    fromViewController.view.frame = fullFrame;

    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:.5 initialSpringVelocity:.5 options:0 animations:^{
        fromViewController.view.frame = CGRectMake(0, fullFrame.size.height, fullFrame.size.width, fullFrame.size.height);
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];

//This is working fine
//        [UIView animateWithDuration:[self transitionDuration:transitionContext]/2       animations:^{
//            fromViewController.view.frame = CGRectMake(0, fullFrame.size.height, fullFrame.size.width, fullFrame.size.height);
//        } completion:^(BOOL finished) {
//            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
//        }];
}

这是手势识别器背后的代码:

#pragma mark - Gesture recognition
- (void)handlePan:(UIScreenEdgePanGestureRecognizer *)pgr
{

CGPoint translation = [pgr translationInView:pgr.view];
CGFloat percentage  = fabs(translation.x / CGRectGetWidth(pgr.view.frame));

switch (pgr.state) {
    case UIGestureRecognizerStateBegan:
        self.interactionInProgress = YES;
        [self.respondingVC proceedToNextViewController];
        break;

    case UIGestureRecognizerStateChanged: {
        [self updateInteractiveTransition:percentage];
        break;
    }

    case UIGestureRecognizerStateEnded:
        if(percentage < 0.5) {
            [self cancelInteractiveTransition];
        } else {
            [self finishInteractiveTransition];
        }
        self.interactionInProgress = NO;
        break;

    case UIGestureRecognizerStateCancelled:
        [self cancelInteractiveTransition];
        self.interactionInProgress = NO;

    default:
        break;
}
}
4

1 回答 1

0

看起来你正在使用UIPercentDrivenInteractiveTransition. 当你打电话finishInteractiveTransition时,UIPercentDriventInteractiveTransition不打电话animateTransition

您应该同时覆盖updateInteractiveTransitionfinishInteractiveTransition更新您的视图状态。

我所有在交互式过渡中开始动画块的尝试都以失败告终。我终于至少通过调用 [ self updateInteractiveTransition:1] in来完成动画finishInteractiveTransition。否则,视图将保持它们所处的状态,并且转换永远不会完成,或者转换被视为已完成,但您的视图位于奇怪的位置。

于 2013-11-14T01:38:30.930 回答