Skip to content

Instantly share code, notes, and snippets.

@veritech
Created April 12, 2011 12:24
Show Gist options
  • Save veritech/915414 to your computer and use it in GitHub Desktop.
Save veritech/915414 to your computer and use it in GitHub Desktop.
Using CAKeyFrameAnimation to animate images in a CALayer
//Create the layer
layer = [CALayer layer];
//Configure the animation
animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
[animation setCalculationMode:kCAAnimationDiscrete];
[animation setDuration:2.0f];
[animation setRepeatCount:HUGE_VALF];
[animation setValues:/* your NSArray of CGImageRefs*/];
//Add the animation to the layer
[layer addAnimation:animation
forKey:@"contents"
];
[layer setFrame:/* The frame of the layer*/];
//Add to the super layer
[parentLayer addSublayer:layer];
@ftvs
Copy link

ftvs commented Feb 23, 2012

Note that each CGImageRef needs to be casted to an id:

    [animationFrames addObject:(id)[uiImageObject CGImage]];

@nicpro85
Copy link

thanks for this gist! my little hero is now an animated layer :)

@binderclip
Copy link

Thanks, it works. And the [animation setValues: /**/]; just like this:

[animation setValues:[NSArray arrayWithObjects:(id)[UIImage imageNamed:@"i1"].CGImage, (id)[UIImage imageNamed:@"i2"].CGImage, (id)[UIImage imageNamed:@"i3"].CGImage, nil]];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment