如何使用CABasicAnimation水平移动球并将球停在正确的位置

我正在尝试写一个游戏,我对这个动画有点困惑。我有一个在我的屏幕上水平动画的球。这是代码:
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”。然后,它将转到gameObject设置的正确值。有人可以帮我这个吗?     
已邀请:
你需要做的是将目标位置设置在禁用动作的位置。例如:
//--------------------------------------------------------------------------------------------------

#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];
}
    

要回复问题请先登录注册