3

当我使用 CAKeyframeAnimation 在屏幕上移动 UIImageView 时遇到间歇性问题。我希望 UIImageView 的位置在动画完成后保持在动画结束的位置。此错误仅发生在某些起点和终点。当我使用随机点时,它大部分时间都能正常工作,但大约 5-15% 的时间它会失败并迅速回到动画前的位置。该问题仅在使用路径属性使用 CAKeyframeAnimation 时出现。如果我使用 values 属性,则不会出现错误。我设置removedOnCompletion = NO,并且fillMode = kCAFillModeForwards。我在下面发布了一个测试 Xcode 的链接。这是我设置动画的代码。我有一个属性 usePath。当这是 YES 时,就会出现错误。当我将 usePath 设置为 NO 时,不会发生回弹错误。

// create the point        
CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
if (self.usePath) {
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, startPt.x, startPt.y);
    CGPathAddLineToPoint(path, NULL, endPt.x, endPt.y);
    moveAnimation.path = path;
    CGPathRelease(path);    
} else {
    moveAnimation.values = [NSArray arrayWithObjects:
                            [NSValue valueWithCGPoint:startPt],
                            [NSValue valueWithCGPoint:endPt],
                            nil];
}
moveAnimation.calculationMode = kCAAnimationPaced;
moveAnimation.duration = 0.5f;
moveAnimation.removedOnCompletion = NO;
// leaves presentation layer in final state; preventing snap-back to original state
moveAnimation.fillMode = kCAFillModeForwards; 
moveAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
// moveAnimation.delegate = self;    

// start the animation
[ball.layer addAnimation:moveAnimation forKey:@"moveAnimation"];

要 dl 并查看我的测试项目 goto test project ( http://www.24x7digital.com/downloads/PathFillModeBug.zip )

点击“移动球”按钮开始球的动画。我已经硬编码了一个起点和终点,这会导致错误每次都发生。使用开关将 usePath 更改为 YES 或 NO。当 usePath 为 YES 时,您将看到 snap back 错误。当 usePath 为 NO 时,您将看不到 snap back 错误。

我正在使用 SDK 3.1.3,但我也看到了使用 SDK 3.0 的这个错误,并且我在 Sim 和我的 iPhone 上看到了这个错误。

任何关于如何解决这个问题或我做错了什么的想法都值得赞赏。谢谢,马克。

4

1 回答 1

7

在联系 idp-dts@apple.com 后,他们确认 Core Animation 中存在关于带有路径的 kCAFillModeForwards 的错误。他们告诉我提交我所做的错误报告(问题 ID:7797921)。

他们确实试图给我一个解决方法。他们说在我开始动画后立即设置结束点位置。当错误发生时,这将防止位置回弹:

// start the animation
[ball.layer addAnimation:moveAnimation forKey:@"moveAnimation"];
ball.layer.position = endPt;

但在我的实际应用程序中,我设置了 rotationMode = kCAAnimationRotateAuto 以便更新对象 z 旋转以使其与路径相切。因此,当回弹发生时,rotationMode z 旋转将丢失。他们告诉我在 animationDidStop:finished: 中明确设置旋转,如下所示:

[ball.layer setValue:[NSNumber numberWithDouble:-M_PI/2.0] forKeyPath:@"transform.rotation.z"];

这并不能完全解决问题,因为有时我会出现快速回旋的闪烁,然后是校正旋转。我希望他们修复 fillMode = kCAFillModeForwards 的路径错误。

于 2010-03-26T16:53:31.343 回答