Home > Articles > Home & Office Computing > Mac OS X

A Look at Apple's Core Animation

  • Print
  • + Share This
Easily one of the most exciting new APIs included with Leopard is Core Animation. With Core Animation, developers can now easily add fluid animation to their applications without having to delve too deeply into the world of OpenGL. Software developer Marcus Zarra walks you through some of the basics of Core Animation and how they can be applied to a normal Cocoa application.
Like this article? We recommend

Apple defines Core Animation as:

A framework that makes it simple for Mac developers to add visually stunning user interfaces, graphics, and animations to applications. Without any advanced graphics techniques, you can create fluid, stutter-free effects and experiences as groundbreaking as Time Machine and the new Dock.

Core Animation allows any developer to add animation, simple or complex, to his or her application without having to learn the complexities of OpenGL. With Core Animation, a developer can define a start point and an end point of animation and then move on. The API will handle all of the frames between those two points using tweening—the process of generating intermediate frames between two images.

First, Core Animation is not compatible with Tiger or any of the earlier cats. This is not a technology that will be back-ported in any way, so if you want to be able to use it then your application must be Leopard only. However, given the adoption rate, it is doubtful that this is going to be an issue.

Core Animation is not a replacement for OpenGL. It is not intended to be used as the graphics layer for a game engine nor is it expected to be used for 3D work. Interestingly enough, it is powerful enough to handle 2D games, and I have no doubt that we will be seeing some games come out that utilize Core Animation instead of OpenGL.

Core Animation is intended as an augmentation for your existing user interface. It is meant to enhance your existing GUI elements to make their meaning more obvious and fluid to the user. Apple still wants us to use the native GUI elements that are a part of Cocoa, but Core Animation allows us to make those elements and the user's interaction smoother. While it is possible to have buttons zooming around your interface, no one wants to see that.

Views and Layers

The most basic aspect of Core Animation is the CALayer. The CALayer is the canvas upon which everything in Core Animation is painted. When you define movement, color changes, image effects, and so on, those are applied to CALayer objects. From a code perspective, CALayers are a lightweight representation similar to an NSView. In fact, NSView objects can be manipulated via their CALayer. This is referred to as being layer-backed.

To accomplish this, you simply need to execute one method call on the NSView:

[myView setWantsLayer:YES];

From that point forward, the NSView will have an associated CALayer that can be accessed via

CALayer *layer = [myView layer];

Once a view has been flagged as "layer-backed", you can manipulate its layer to produce effects on the view itself. For instance, to give a view rounded corners, you can perform the following:

CALayer *layer = [myView layer];
[layer setCornerRadius:1.0f];

In addition to backing a view, layers can also be used by themselves without an accompanying view. In that situation, you can set the content of the layer to tell it what to display. For instance, to have a layer display an image, you would use this code:

CALayer *imageLayer = [CALayer layer];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"jpg"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
CGImageSourceRef emptySource = CGImageSourceCreateWithURL((CFURLRef)fileURL, NULL);
CGImageRef emptyImage = CGImageSourceCreateImageAtIndex(emptySource, 0, NULL);
[imageLayer setContent:(id)emptyImage];


  • + Share This
  • 🔖 Save To Your Account