Pan Gesture的动画从初始状态开始,而不是从当前状态开始

时间:2014-08-02 17:38:27

标签: ios uiviewanimation

在我的应用程序中,我有一个位于另一个视图顶部的视图。用户可以向右滑动顶视图,将其淡出并“淡入”后视图。

当用户向右平移并松开时,动画将启动并且应该从当前位置转换到新的屏幕外位置。问题是动画会将视图重新捕捉到其起点,然后进行动画制作。

这是我的平移手势和动画的代码:

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

我已尝试将动画选项设置为:

UIViewAnimationOptionBeginFromCurrentState

没有效果。

任何人都知道我错过了什么?感谢

1 个答案:

答案 0 :(得分:1)

凯尔,如果你使用自动布局,有时会发生这种情况。如果您不需要它,请尝试禁用它并查看它是否已修复它。