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

时间:2010-11-23 17:15:04

标签: iphone calayer cabasicanimation caanimation

我正在尝试编写一个游戏而且我对此动画有点困惑。我有一个在我的屏幕上水平动画的球。这是代码:

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设置的正确值。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分: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];
}
相关问题