1

In my application I have a view that sits on top of another view. The user is able to swipe the top view to the right, which fades it out and "fades in" the back view.

When the user pans to the right and lets go, the animation kicks in and it should translate from its current position to the new off screen position. The problem is that the animation instead snaps the view back to its starting point and then does the animation.

Here is my code for the pan gesture and animation:

- (void)handlePanGesture:(UIPanGestureRecognizer *)recognizer
{
    CGPoint translation = [recognizer translationInView:recognizer.view];
    CGPoint velocity = [recognizer velocityInView:recognizer.view];

    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan: {
            [recognizer setTranslation:CGPointMake(self.slidingView.frame.origin.x, 0) inView:recognizer.view];
            break;
        }
        case UIGestureRecognizerStateChanged: {
            [self.slidingView setTransform:CGAffineTransformMakeTranslation(MAX(0,translation.x), 0)];

            CGFloat percentage = fmaxf(0,(translation.x/recognizer.view.bounds.size.width));

            [self.backgroundBlurImageView setAlpha:1-percentage];
            [self.slidingView setAlpha:1-percentage];

            [self.backgroundImageView setTransform:CGAffineTransformMakeScale(1 + (percentage * 0.05), 1 + (percentage * 0.05))];
            [self.backgroundBlurImageView setTransform:CGAffineTransformMakeScale(1 + (percentage * 0.05), 1 + (percentage * 0.05))];

            break;
        }
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled: {
            if (velocity.x > 5.0 || (velocity.x >= -1.0 && translation.x > kMenuViewControllerMinimumPanDistanceToOpen * self.slidingView.bounds.size.width)) {
                CGFloat transformedVelocity = velocity.x/ABS(self.slidingView.bounds.size.width - translation.x);
                CGFloat duration = 0.66;
                [self showViewAnimated:YES duration:duration initialVelocity:transformedVelocity];
            } else {
                [self hideViewAnimated:YES];
            }
        }
        default:
            break;
    }

}


- (void)showViewAnimated:(BOOL)animated duration:(CGFloat)duration
         initialVelocity:(CGFloat)velocity;
{
    // animate
    __weak typeof(self) blockSelf = self;
    [UIView animateWithDuration:animated ? duration : 0.0 delay:0
         usingSpringWithDamping:0.8f initialSpringVelocity:velocity options:UIViewAnimationOptionAllowUserInteraction animations:^{
             blockSelf.slidingView.transform = CGAffineTransformMakeTranslation(blockSelf.slidingView.bounds.size.width, 0);
             [blockSelf.backgroundBlurImageView setTransform:CGAffineTransformMakeScale(1.05, 1.05)];
             [blockSelf.backgroundImageView setTransform:CGAffineTransformMakeScale(1.05, 1.05)];
             [blockSelf.backgroundBlurImageView setAlpha:0];
             [blockSelf.slidingView setAlpha:0];
         } completion:^(BOOL finished) {
             blockSelf.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapOnAaron:)];
             [blockSelf.view addGestureRecognizer:self.tapGestureRecognizer];
         }];
}

- (void)hideViewAnimated:(BOOL)animated;
{
    __weak typeof(self) blockSelf = self;
    [UIView animateWithDuration:0.3f animations:^{
        blockSelf.slidingView.transform = CGAffineTransformIdentity;
        [blockSelf.backgroundBlurImageView setTransform:CGAffineTransformIdentity];
        [blockSelf.backgroundImageView setTransform:CGAffineTransformIdentity];
        [blockSelf.backgroundBlurImageView setAlpha:1];
        [blockSelf.slidingView setAlpha:1];
    } completion:^(BOOL finished) {
        [blockSelf.view removeGestureRecognizer:self.tapGestureRecognizer];
        blockSelf.tapGestureRecognizer = nil;
    }];
}

I have already tried settings the animation options to:

UIViewAnimationOptionBeginFromCurrentState

with no effect.

Anyone have a clue what I am missing? Thanks

Kyle Begeman
  • 7,169
  • 9
  • 40
  • 58
  • Kyle, sometimes this sort of thing happens if you are using autolayout. If you don't need it try disabling it and see if it fixes it. – rymagno Aug 02 '14 at 19:26
  • @user2608440 That did the trick! I don't need auto layout for this project so the simple solution was to uncheck that option. For future projects where I will be utilizing auto layout, I wonder what can be done to avoid this. Thanks a lot for the input! If you want to post this as an "Answer" instead of just a comment, I will select it so you get credit – Kyle Begeman Aug 02 '14 at 23:32
  • Kyle, take a look here for transforms and autolayout: http://stackoverflow.com/questions/12943107/how-do-i-adjust-the-anchor-point-of-a-calayer-when-auto-layout-is-being-used/14105757#14105757 – rymagno Aug 02 '14 at 23:35

1 Answers1

1

Kyle, sometimes this sort of thing happens if you are using autolayout. If you don't need it try disabling it and see if it fixes it.

rymagno
  • 458
  • 2
  • 9