0

我正在尝试编写游戏,但我对这个动画有点卡住了。我的屏幕上有一个水平动画的球。这是代码:

CABasicAnimation *horizontalBallAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
[horizontalBallAnimation setFromValue:[NSValue valueWithCGPoint:CGPointMake(50.0, 300.0 )]];
[horizontalBallAnimation setToValue:[NSValue valueWithCGPoint:CGPointMake(220.0, 300.0 )]];

horizontalBallAnimation.delegate = self;
horizontalBallAnimation.repeatCount = 1e100f;
//horizontalBallAnimation.autoreverses = YES;
horizontalBallAnimation.duration = 2.0;

[gameBallLayer addAnimation:horizontalBallAnimation forKey:@"horizontalBallAnimation"];

这很好用。然而我的问题是,当我停止动画时——只需点击屏幕——球在到达正确的位置之前;

[gameLogic ballGameObject].x = [[[gameBallLayer presentationLayer] valueForKeyPath:@"position.x"] doubleValue];
    [gameLogic ballGameObject].y = [[[gameBallLayer presentationLayer] valueForKeyPath:@"position.y"] doubleValue];

首先对 Horizo​​ntalBallAnimation 的“ToValue”进行动画处理。然后,它转到由游戏对象设置的正确值。有人可以帮我解决这个问题吗?

4

1 回答 1

0

您需要做的是在CATransaction禁用操作的范围内设置目标位置。例如:

//--------------------------------------------------------------------------------------------------

#pragma mark - CAAnimation Delegate Methods 

//--------------------------------------------------------------------------------------------------

- (void)animationDidStart:(CAAnimation *)theAnimation
{
    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue
                     forKey:kCATransactionDisableActions];

    // Set the layer's target position here.        

    [CATransaction commit];
}
于 2011-11-09T18:03:34.843 回答