Skip to content

CAKeyframeAnimation path animation #1

@Jason2510

Description

@Jason2510

CAKeyframeAnimation path animation is working fine in demo but when i use that function in my project its not working. can you plz help what is the problem?

Here is my code,
UIBezierPath* circularPath = [UIBezierPath bezierPath];
[circularPath moveToPoint:CGPointMake(50, 300)];
[circularPath addCurveToPoint:CGPointMake(300, 300) controlPoint1:CGPointMake(100, 50) controlPoint2:CGPointMake(250, 450)];
CAShapeLayer* layerShape = [CAShapeLayer layer];
layerShape.lineWidth = 2.0f;
layerShape.fillColor = [UIColor clearColor].CGColor;
layerShape.strokeColor = [UIColor redColor].CGColor;
layerShape.path = circularPath.CGPath;
[self.view.layer addSublayer:layerShape];

layer = [CALayer layer];
layer.frame = CGRectMake(0, 0, 20, 20);
layer.position = CGPointMake(50, 300);
layer.backgroundColor = [UIColor greenColor].CGColor;
[self.view.layer addSublayer:layer];

CABasicAnimation* colorChangeAnimation = [CABasicAnimation animation];
colorChangeAnimation.keyPath = @"backgroundColor";
colorChangeAnimation.toValue = (__bridge id) [UIColor blueColor].CGColor;

//Usually use this approach for perform rotation while animating stuff
CABasicAnimation* rotationAnimation = [CABasicAnimation animation];
rotationAnimation.keyPath = @"transform.rotation";
rotationAnimation.byValue = @(M_PI*2);

CAKeyframeAnimation* animation = [CAKeyframeAnimation animation];
animation.keyPath = @"position";
animation.path = circularPath.CGPath;
animation.rotationMode = kCAAnimationRotateAuto;

CAAnimationGroup* animationGroup = [CAAnimationGroup animation];
animationGroup.animations = @[colorChangeAnimation, animation, rotationAnimation];
animationGroup.duration = 1.0f;
animationGroup.delegate = self;
animationGroup.autoreverses = NO;
animationGroup.removedOnCompletion = YES;
layer.position = CGPointMake(300, 300);
[layer addAnimation:animationGroup forKey:@"anim"];

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions