我正在使用 aUIViewPropertyAnimator
为 a 的框架设置动画UICollectionViewCell
。我查看平移手势识别器的速度,以决定动画师是应该自然完成还是反转并返回到初始状态。
在所有模拟器中,在我的 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];
}