Rob Rob -4 years ago 141
iOS Question

UIPercentDrivenInteractiveTransition yielding extraneous animation when done

I'm using an interactive custom push transition with a

UIPercentDrivenInteractiveTransition
. The gesture recognizer successfully calls the interaction controller's
updateInteractiveTransition
. Likewise, the animation successfully completes when I call the interaction controller's
finishInteractiveTransition
.

But, sometimes I get a extra bit of distracting animation at the end (where it seems to repeat the latter part of the animation). With reasonably simple animations, I rarely see this symptom on the iPhone 5 (though I routinely see it on the simulator when working on slow laptop). If I make the animation more computationally expensive (e.g. lots of shadows, multiple views animating different directions, etc.), the frequency of this problem on the device increases.

Has anyone else seen this problem and figured out a solution other than streamlining the animations (which I admittedly should do anyway) and/or writing my own interaction controllers? The
UIPercentDrivenInteractiveTransition
approach has a certain elegance to it, but I'm uneasy with the fact that it misbehaves non-deterministically. Have others seen this behavior? Does anyone know of other solutions?

To illustrate the effect, see the image below. Notice how the second scene, the red view, when the animation finishes, seems to repeat the latter part of its animation a second time.

animation not right

This animation is generated by:


  • repeatedly calling
    updateInteractiveTransition
    , progressing update from 0% to 40%;

  • momentarily pausing (so you can differentiate between the interactive transition and the completion animation resulting from
    finishInteractiveTransition
    );

  • then calling
    finishInteractiveTransition
    to complete the animation; and

  • the animation controller's animation's
    completion
    block calls
    completeTransition
    for the
    transitionContext
    , in order to clean everything up.



Doing some diagnostics, it appears that it is this last step that triggers that extraneous bit of animation. The animation controller's completion block is called when the animation is finished, but as soon as I call
completeTransition
, it sometimes repeats the last bit of the animation (notably when using complex animations).




I don't think it's relevant, but this is my code for configuring the navigation controller to perform interactive transitions:

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


My
PushAnimator
is:

@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


Note, when I put logging statement where I call
completeTransition
, I can see that this extraneous bit of animation happens after I call
completeTransition
(even though the animation was really done at that point). This would suggest that that extra animation may have been a result of the call to
completeTransition
.

FYI, I've done this experiment with a gesture recognizer:

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


I also did it by calling the
updateInteractiveTransition
and
finishInteractiveTransition
manually (eliminating the gesture recognizer from the equation), and it still exhibits this strange behavior:

[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, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.interactionController finishInteractiveTransition];
});
});


Bottom line, I've concluded that this is a problem isolated to
UIPercentDrivenInteractiveTransition
with complex animations. I can minimize the problem by simplifying them (e.g. snapshotting and animated snapshotted views). I also suspect I could solve this by not using
UIPercentDrivenInteractiveTransition
and writing my own interaction controller, which would do the animation itself, without trying to interpolate the
animationWithDuration
block.

But I was wondering if anyone has figured out any other tricks to using
UIPercentDrivenInteractiveTransition
with complex animations.

Answer Source

I've seen something similar. I have two possible workarounds. One is to use delayed performance in the animation completion handler:

} 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, dispatch_get_main_queue(), ^(void){
            BOOL cancelled = [transitionContext transitionWasCancelled];
            [transitionContext completeTransition:!cancelled];
        });
       self.interacting = NO;
}];

The other possibility is: don't use percent-drive animation! I've never had a problem like this when driving the interactive custom animation myself manually.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download