What I basically want to do is tp display a sequence of images at a fast rate (60/second) in order to create a sequence. I'm going to create a timer and see if it's fast enough.
I want to display an array of images in my cocoa app using an animation (up to 60 per seconds).
Here's the animation I've created so far:
- (CAAnimation*)imageAnimation;
{
CGImageRef image1 = [self nsImageToCGImageRef:[NSImage imageNamed:@"1.png"]];
CGImageRef image2 = [self nsImageToCGImageRef:[NSImage imageNamed:@"2.png"]];
CGImageRef image3 = [self nsImageToCGImageRef:[NSImage imageNamed:@"3.png"]];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
NSArray *images = [NSArray arrayWithObjects:(id)image1, (id)image2, (id)image3, nil];
[animation setValues:images];
[animation setDuration:3.0];
return animation;
}
where nsImageToCGImageRef
is a method I've defined elsewhere. Now, I'm not sure where to go from here to add this animation to my views.
Well, to start with, the question is not clear enough. You may want to rephrase as the question does not say what you want to achieve clearly and does not say anything about what you have done till now with the code.
CAKeyframeAnimation is used to specify the values of a property of a layer during the time of animation.
You are creating key frame animation with property position; in that case, you should give position values of the layer to be set during the animation. You are trying to change the images. From what I could understand from your code, you should set contents property instead of animation property with the images values but you won't get smooth animation. I have not tried this myself but based on theory, its what is needed. Is your code working at all? I don't think so in the way you require even if its slow? Please let me know if changing property name "position" for message animationWithKeyPath to "contents" works even if its slow?
You will need to have a textured atlases for that. Take a look at url http://mysterycoconut.com/blog/2011/01/cag1/
That may show what you need based on what I could understand from your question.
After some research and trial it seems like the best way is to use a timer instead, and change the image with the next available.
You should investigate
CVDisplayLink
, which allows you to receive a callback every time the Mac's display subsystem is about to display a frame. This will let you display images at 60fps with no dropped frames, because the API only calls your code when it needs an update.This means you don't need to worry about synchronising a timer with the display, and it also means that you are not doing unnecessary work. You can also do things such as update only on alternate callbacks to reduce the frame rate to 30fps if your drawing code is slow.