反转UIViewPropertyAnimator会导致帧动画问题

时间:2017-05-02 18:43:26

标签: ios objective-c animation uikit uiviewpropertyanimator

我正在使用UIViewPropertyAnimatorUICollectionViewCell的框架设置动画。我看一下平移手势识别器的速度,以决定动画师是自然完成还是反转并回到初始状态。

在所有模拟器中,在我的iPhone 5s以及6s +上,这些动画运行完美无瑕。然而,在我的iPhone 7+上,每当我反转动画时,我都会发现奇怪的帧闪烁。请参阅下面的代码,了解我的工作方式。对iPhone 7+的影响是,一旦我设置reversed = YES然后调用continueAnimationWithTimingParameters:durationFactor:,框架会立即跳转到屏幕的完全不同的部分,然后从那里运行反转的动画。只有在动画完成运行后,帧才会跳回原本应该恢复的位置。

我试图删除使用弹簧计时参数,但这并没有什么区别。

这是代码的抽象版本:

- (void)prepareAnimation {
    // Called when user begins panning in certain direction
    // ...
    self.cardFrameAnimator = [[UIViewPropertyAnimator alloc] initWithDuration:0.5 dampingRatio:0.8 animations:^{
        [self currentCell].frame = targetFrame;
    }];
}

- (void)panningEndedWithTranslation:(CGPoint)translation velocity:(CGPoint)velocity
{
    if (self.cardFrameAnimator.isRunning)
    {
        return;
    }

    CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
    CGVector velocityVector = CGVectorMake(velocity.x / 500, velocity.y / 500);

    __weak CardStackCollectionViewController *weakSelf = self;

    switch (self.currentState) {
        case CurrentStateStacked:
            if (translation.y <= -screenHeight / 3 || velocity.y <= -100)
            {
                // Let the animation run to completion
                self.cardFrameAnimator.reversed = NO;
                [self setCurrentCellsCornerRadius:0];
                [self.cardFrameAnimator addCompletion:^(UIViewAnimatingPosition finalPosition) {
                    weakSelf.activeCellState = CurrentStateFullscreen;
                }];
            }
            else
            {
                // Revert the animation back to the default state
                self.cardFrameAnimator.reversed = YES;
                [self setCurrentCellsCornerRadius:20];
                [self.cardFrameAnimator addCompletion:^(UIViewAnimatingPosition finalPosition) {
                    weakSelf.activeCellState = CurrentStateStacked;
                }];
            }
            break;
        case CurrentStateFullscreen:
            if (translation.y >= screenHeight / 3 || velocity.y >= 100)
            {
                // Let the animation run to completion
                self.cardFrameAnimator.reversed = NO;
                [self setCurrentCellsCornerRadius:20];
                [self.cardFrameAnimator addCompletion:^(UIViewAnimatingPosition finalPosition) {
                    weakSelf.activeCellState = CurrentStateStacked;
                }];
            }
            else
            {
                // Revert the animation back to the default state
                self.cardFrameAnimator.reversed = YES;
                [self setCurrentCellsCornerRadius:0];
                [self.cardFrameAnimator addCompletion:^(UIViewAnimatingPosition finalPosition) {
                    weakSelf.activeCellState = CurrentCellStateFullscreen;
                }];
            }
            break;
    }

    UISpringTimingParameters *springParameters = [[UISpringTimingParameters alloc] initWithDampingRatio:0.8 initialVelocity:velocityVector];
    [self.cardFrameAnimator continueAnimationWithTimingParameters:springParameters durationFactor:1.0];
}

0 个答案:

没有答案