Search code examples
iphoneipadcore-animationcalayer

Strange behaviour of CALayer


Iv animated a CALayer to move along a curve. Once the animation is done i hide the layer in the animationDidStop delegate. Say i set the layer's frame.origin to be at point x. And i animate the Layer to move from point x to point y. Once the animation stops i set the layer to be hidden. However, the layer appears once at point x again before getting hidden. Why is this happening? How can i stop this and hide the layer immediately after animation stops when it reaches point y?

-(void)doAnimation  
{  
UIImage *movingImage = [UIImage imageNamed:@"XYZ.png"];  
movingLayer = [CALayer layer];   
 movingLayer.contents = (id)movingImage.CGImage;  
 movingLayer.anchorPoint = CGPointZero;  
 movingLayer.frame = CGRectMake(700.0f, 50.0f, movingImage.size.width, movingImage.size.height);  
 [self.view.layer addSublayer:movingLayer];

UIBezierPath *customPath = [UIBezierPath bezierPath];  
[customPath moveToPoint:CGPointMake(700,50)];  
[customPath addQuadCurveToPoint:CGPointMake(550, 50) controlPoint:CGPointMake(630, 10)];  
[customPath addQuadCurveToPoint:CGPointMake(270, 33) controlPoint:CGPointMake(355, -10)];  
[customPath addQuadCurveToPoint:CGPointMake(120, 0) controlPoint:CGPointMake(190, 0)];   
[customPath addLineToPoint:CGPointMake(-20, 20)];  

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];  
 pathAnimation.duration = 6.5f;  
 pathAnimation.path = customPath.CGPath;  
 pathAnimation.calculationMode = kCAAnimationLinear;  
 pathAnimation.delegate=self;  
 [movingLayer addAnimation:pathAnimation forKey:@"movingAnimation"];

}


-(void)animationDidStop:(CAAnimation *)animID finished:(BOOL)didFinish  
{  
movingLayer.hidden=YES;  
}

Solution

  • When you start the animation you should set the position of the layer to the destination.