Question

I have been trying to do CALayer animation. But after animation it reverts back.I found a few solutions like removedOnCompleteion = YES and fill mode = kCAFillModesForwards but none is helping me. here is the code. Need a little help fellows thx.

  void(^ myblock)(NSDictionary *) = ^(NSDictionary *dict){

        NSMutableArray *arrOfPt=       [dict objectForKey:@"Path"];

        CGPoint p1 = [[arrOfPt objectAtIndex:0] CGPointValue];

        CGPoint cp1 = [[arrOfPt objectAtIndex:1] CGPointValue];

//        CGPoint cp1 = [[arrOfPt objectAtIndex:1] CGPointValue];
        CGPoint cp2 = [[arrOfPt objectAtIndex:2] CGPointValue];
        CGPoint p2 = [[arrOfPt objectAtIndex:3] CGPointValue];

        NSMutableArray *arrayTime =  [dict objectForKey:@"Time"];
        CGFloat time = [[arrayTime objectAtIndex:0] floatValue];

        pointerLayer.opacity = 1;
//        NSArray *oneObjPt = [NSArray arrayWithObjects:[arrOfPt objectAtIndex:0],[arrOfPt lastObject],nil];
        //         NSArray *oneObjTime = [NSArray arrayWithObjects:[arrayTime objectAtIndex:0],[arrayTime lastObject],nil];
//        NSArray *oneObjTime = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:1.0],nil];
        CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

//        [keyFrameAnimation setCalculationMode:kCAAnimationPaced];
        keyFrameAnimation.duration = 1;
        keyFrameAnimation.removedOnCompletion = NO;
        [keyFrameAnimation setFillMode:kCAFillModeForwards];

        CGMutablePathRef fpath = CGPathCreateMutable();

        CGPathMoveToPoint(fpath, NULL, p1.x, p1.y);
        CGPathAddCurveToPoint(fpath, NULL, cp1.x, cp1.y, cp2.x, cp2.y, p2.x, p2.y);
        keyFrameAnimation.path = fpath;

        [pointerLayer addAnimation:keyFrameAnimation forKey:@"pos"];
        CGPoint newLoc = pointerLayer.position;// this is just to verify the layer's location
Was it helpful?

Solution

I found a few solutions like removedOnCompleteion = YES and fill mode = kCAFillModesForwards

Those are not solutions.

CGPoint newLoc = pointerLayer.position;

That won't do any good; adding an animation does not actually change the position. The animation happens later.

I'm not entirely sure what you're after, but it might help you to read my explanation of Core Animation. Animation is just an illusion; it's a game played with the "presentation layer". It is up to you to move the real layer to where it is going to be after the animation and to add the animation that will appear to make it move there.

I provide a formulaic approach for making sure that your Core Animation works to get you where you want to go:

http://www.apeth.com/iOSBook/ch17.html#_using_a_cabasicanimation

OTHER TIPS

I can't seem to recreate this - granted I used some test data:

CGPoint cp1 = CGPointMake(0, 0);
CGPoint cp2 = CGPointMake(100, 100);
CGPoint cp3 = CGPointMake(85, 150);
CGPoint cp4 = CGPointMake(50, 50);
CGFloat time = 2.0f;
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyFrameAnimation.duration = time;
keyFrameAnimation.removedOnCompletion = NO;
[keyFrameAnimation setFillMode:kCAFillModeForwards];

CGMutablePathRef fpath = CGPathCreateMutable();

CGPathMoveToPoint(fpath, NULL, cp1.x, cp1.y);
CGPathAddCurveToPoint(fpath, NULL, cp2.x, cp2.y, cp3.x, cp3.y, cp4.x, cp4.y);
keyFrameAnimation.path = fpath;
[testView.layer addAnimation:keyFrameAnimation forKey:@"pos"];
CFRelease(fpath);

which outputs:

http://glassofcocoa.com/code/AnimationTest.mp4

Possibly one of your points in the array might be the starting position?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top