使用CAKeyframeAnimation移动到几点

时间:2013-05-03 21:40:34

标签: objective-c cakeyframeanimation

我有代码:

NSMutableArray * pathArray = [[NSMutableArray alloc]init];
CGPoint currentPoint = CGPointMake(xp, yp);
[pathArray addObject:[NSValue valueWithCGPoint: currentPoint]];

NSMutableArray * pathArray = [algorithm CreatePath];
CGMutablePathRef path = CGPathCreateMutable();

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.values = pathArray;
pathAnimation.path = path;
pathAnimation.duration = 1.0;
[self.firstBall.ballLayer addAnimation:pathAnimation forKey:@"position"];

在路径数组中,我在NSValue中包含很少的CGPoints。但是当动画从第一个坐标到最后一个坐标时,它只会移动吗为什么不使用其他要点?

新代码:

-(void)MoveBallWithAlgorithm:(CGPoint)start end:(CGPoint)end;
{
    FindWayAlgorithm *algorithm = [[FindWayAlgorithm alloc]init];
    algorithm.LX = algorithm.LY = self.columns;

    [algorithm CreateBoard:self.fieldsArray];
    [algorithm FindWay:start end:end];
    CGMutablePathRef path = CGPathCreateMutable();
    NSMutableArray * pathArray = [algorithm CreatePath];

       @try
    {
        CGPathMoveToPoint(path, NULL, self.firstBall.ballCoordinates.x, self.firstBall.ballCoordinates.y);
        for (NSValue * pointValue in pathArray) {
            CGPoint point = [pointValue CGPointValue];
            Field* field = [self FindFieldWithPoint:point];
            CGPathAddLineToPoint(path, NULL, field.ballCoordinates.x, field.ballCoordinates.y);
        }
    }
    @catch(NSException* ex)
    {
        NSLog(@"Bug captured when move ball with algorithm: %@ %@",ex, [NSThread callStackSymbols]);
    }
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.duration = 2.0;
    pathAnimation.path = path;
    [pathAnimation setDelegate:self];
    [self.firstBall.ballLayer addAnimation:pathAnimation forKey:@"position"];
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{

    NSInteger firstBallIndex = [self.fieldsArray indexOfObject:self.firstBall];
    NSInteger secondBallIndex = [self.fieldsArray indexOfObject:self.secondBall];
    self.ballFrom = [self.fieldsArray objectAtIndex:firstBallIndex];
    self.ballTo = [self.fieldsArray objectAtIndex:secondBallIndex];
    self.ballTo.ballLayer = self.ballFrom.ballLayer;
    CGPoint endPt = CGPointMake(self.secondBall.ballCoordinates.x,self.secondBall.ballCoordinates.y);

    self.ballTo.ballLayer.frame = CGRectMake(endPt.x, endPt.y, self.ballSize, self.ballSize);
    self.ballFrom.ballLayer = nil;
    [self.fieldsArray replaceObjectAtIndex:firstBallIndex withObject:self.ballFrom];
    [self.fieldsArray replaceObjectAtIndex:secondBallIndex withObject:self.ballTo];
    self.firstBall = nil;
    self.secondBall = nil;
}

2 个答案:

答案 0 :(得分:4)

我的问题已经解决了。我补充说:

pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;

现在我的图层没有回到第一个位置;)

答案 1 :(得分:2)

CAKeyframeAnimation文档声明:

  

指定path会覆盖values属性。

因此,如果要设置关键帧值,则不应设置pathAnimation.path pathAnimation.values = pathArray

我用一个简单的标签和以下代码测试了它:

NSArray * pathArray = @[
    [NSValue valueWithCGPoint:CGPointMake(10., 10.)],
    [NSValue valueWithCGPoint:CGPointMake(100., 10.)],
    [NSValue valueWithCGPoint:CGPointMake(10., 100.)],
    [NSValue valueWithCGPoint:CGPointMake(10., 10.)],
];

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.values = pathArray;
pathAnimation.duration = 5.0;
[self.label.layer addAnimation:pathAnimation forKey:@"position"];