1

I have code :

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"];

And in path array I have few CGPoints wrapped in NSValue. But when animation go its only move from first to last coordinate? Why it not use other points?

New code:

-(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
  • Please fix the code formatting in your question, and add the code that adds lines or curves to the path.
    – rob mayoff
    May 3, 2013 at 21:43
  • Which of methods I should use to my animation move from few points, one to another ? I am beginner in animation :/
    – netmajor
    May 3, 2013 at 22:00

2 Answers 2

4

My problem is resolved. I add:

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

Now my layer not back to first position ;)

1
  • Hey I just have to say that I've been searching forever for this answer. Thank you SO MUCH for posting this.
    – Josh Gafni
    Jul 29, 2015 at 4:07
2

The CAKeyframeAnimation documentation states:

Specifying a path overrides the values property.

so you should not set pathAnimation.path if you want to set the keyframe values with pathAnimation.values = pathArray.

I tested it with a simple label and the following code:

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"];
9
  • @netmajor: I tried your code and after removing pathAnimation.path = path it animated an object through all positions in the pathArray.
    – Martin R
    May 3, 2013 at 22:10
  • @netmajor: Your pathArray contains only one point (the current point), not multiple points.
    – Martin R
    May 3, 2013 at 22:22
  • No, I only show You how I add object. I add other values in other piece of code ;)
    – netmajor
    May 3, 2013 at 22:23
  • @netmajor: Then I have no idea. It worked on my small test app.
    – Martin R
    May 3, 2013 at 22:38
  • In your test what do You use ad ballLayer ? Becouse Ichange my code ad above and get error : void CGPathAddLineToPoint(CGMutablePathRef, const CGAffineTransform *, CGFloat, CGFloat): no current point.
    – netmajor
    May 3, 2013 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.