2011-03-23 50 views
0

我一直在我的棋盘游戏iPhone应用程序动画有很多问题。我用下面的函数来设置动画的计算机游戏者移动的计数器:问题使用CABasicAnimation动画多个CALayers

-(void)animateCounterMoveFor:(int)playerType counterId:(int)countId { 

    // set up the move to an end point 
    CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position"]; 
    [move setDuration:3.5]; 
    [move setToValue:[NSValue valueWithCGPoint:CGPointMake((xPos), (yPos))]]; 
    [move setFillMode:kCAFillModeForwards]; 
    [move setRemovedOnCompletion:NO]; 
    CAMediaTimingFunction *tf = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
    [move setTimingFunction:tf]; 

    // enable the animation stop to set layer to right position 
    [move setValue:[NSNumber numberWithInt:countId] forKey:@"counterId"]; 
    [move setValue:[NSNumber numberWithInt:playerType] forKey:@"playerType"]; 
    [move setDelegate:self]; 

    // add move to the layer 
    [layer addAnimation:move forKey:@"moveAnimation"]; 

} 

..然后委托设置层位置,并且当其完成(否则计数器移回到它的起始位置)移除动画。

-(void)animationDidStop:(CABasicAnimation *)anim finished:(BOOL)flag { 

    NSNumber *counterId = [anim valueForKey:@"counterImageId"]; 
    NSNumber *playerId = [anim valueForKey:@"playerType"]; 

    int countId = [counterId intValue]; 
    int playId = [playerId intValue]; 

// code here gets position information using counter and player id 

    // set the final position of the layer when the anim stops, then remove the anim 
    [layer setPosition:CGPointMake(xPos, yPos)]; 
    [layer removeAnimationForKey:@"moveAnimation"]; 

} 

这一切工作正常,当一个计数器正在移动,但是当一个计数器采取另一个我用同样的方法来移动两个。我认为动画的一个副本被传递给图层,所以我可以调用它来移动第一个图层,然后直接调用它,以便移动第二个图层。我注销对象标识符并创建两个不同的移动动作,但只有第二个计数器移动。 animationDidStop:被调用两次,并且每次都传递正确的计数器信息。

有关如何使其工作的任何想法?我已经浏览了很多CAAnimation/CALayer问题,所以我猜它不仅仅是我有问题;)

回答

2

为什么不提前知道最终位置?

我只是在animateCounterMoveFor:方法中设置对象的位置,否则它会回弹到原来的位置,因为calayer实际上并没有移动,直到你设置它。

-(void)animateCounterMoveFor:(int)playerType counterId:(int)countId { 

    // set up the move to an end point 
    CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position"]; 
    [move setDuration:3.5]; 
    [move setToValue:[NSValue valueWithCGPoint:CGPointMake((xPos), (yPos))]]; 
    [move setFillMode:kCAFillModeForwards]; 
    [move setRemovedOnCompletion:NO]; 
    CAMediaTimingFunction *tf = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
    [move setTimingFunction:tf]; 


    [layer addAnimation:move forKey:@"position"]; // Replaces the previous position animation on the layer 
    [layer setPosition:CGPointMake(xPos, yPos)];  // Sets the final position after animation is finished 

    // enable the animation stop to set layer to right position 
    [move setValue:[NSNumber numberWithInt:countId] forKey:@"counterId"]; 
    [move setValue:[NSNumber numberWithInt:playerType] forKey:@"playerType"]; 
    [move setDelegate:self]; 

} 
+0

当我试图设置在animateCounterMoveFor位置:法柜台抢购直奔终点位置,然后跑到离终点POS到底POS动画(我认为),所以没有运动发生。这就是为什么在我发布这个问题的同时做了很多检查我的代码之后,我对animationDidStop: – thiefbagginses 2011-03-30 09:20:06

+0

进行了仔细研究,之后我以更好的视角回到了它,并检查了所有的假设 - 我传递给玩家,反编号错误的方式...呃..所以实际上这个代码确实工作。尽管可能有更好的方法! – thiefbagginses 2011-03-30 09:24:49

+0

您可能需要设置fromValue。 – 2011-03-30 13:57:19