当我使用 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 上看到了这个错误。
任何关于如何解决这个问题或我做错了什么的想法都值得赞赏。谢谢,马克。