Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [126]

By Root 872 0
*)image

{

CGRect superlayerBounds = view.layer.bounds;

NSPoint center = NSMakePoint(CGRectGetMidX(superlayerBounds),

CGRectGetMidY(superlayerBounds));

NSRect imageBounds = NSMakeRect(0, 0, image.size.width,

image.size.height);

CGPoint randomPoint = CGPointMake(

CGRectGetMaxX(superlayerBounds) *

(double)random() / (double)RAND_MAX,

CGRectGetMaxY(superlayerBounds) *

(double)random() / (double)RAND_MAX);

CAMediaTimingFunction *tf = [CAMediaTimingFunction

functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

CABasicAnimation *posAnim = [CABasicAnimation animation];

posAnim.fromValue = [NSValue valueWithPoint:center];

posAnim.duration = 1.5;

posAnim.timingFunction = tf;

CABasicAnimation *bdsAnim = [CABasicAnimation animation];

bdsAnim.fromValue = [NSValue valueWithRect:NSZeroRect];

bdsAnim.duration = 1.5;

bdsAnim.timingFunction = tf;

CALayer *layer = [CALayer layer];

layer.contents = image;

layer.actions = [NSDictionary dictionaryWithObjectsAndKeys:

posAnim, @"position",

bdsAnim, @"bounds", nil];

[CATransaction begin];

[view.layer addSublayer:layer];

layer.position = randomPoint;

layer.bounds = NSRectToCGRect(imageBounds);

[CATransaction commit];

}

That’s it! Run the application. You should see ten images animate out from the center of the window.

Implicit Animation and Actions


Before we talk about what’s going on in presentImage:, let’s look at how the most basic animation is done with Core Animation. Imagine that you have a CALayer called layer that is displayed on the screen. Suppose that you set its position:

layer.position = CGPointMake(50, 50);

This simple action animates the layer from its current position to the new position: implicit animation. Many properties of layers can be animated by simply setting them. The setText: method uses implicit animation to change the size of the black status bubble.

What if we want to customize these animations? As it turns out, there are several styles for achieving customization, which can make Core Animation rather confusing. The most straightforward method is by means of CALayer’s actions property, which takes a dictionary. The actions dictionary associates properties (string keys) with the animation (CAAnimation subclass) to be used when animating that property. The actions dictionary is used by Core Animation to determine what to do when a property is assigned.

CABasicAnimation is, well, the most basic animation class. It has two important properties: fromValue and toValue. By setting one or both of these, you will influence the start and end values of the property you are animating. In presentImage:, you will note that we set only the fromValue. Thus, later in the method when we assign property and bounds, Core Animation can look in the actions dictionary to determine what animation to use for those properties. Because only fromValue is set, the properties will be animated from fromValue to the value that we assigned. Specifically, we animate the position from the center to a random point and the bounds from zero to the size of the thumbnail, simultaneously.

Note that to have the layer display the image, we simply assign it to the contents property. The contentsGravity property affects how contents is scaled (or not scaled). In this application, we have sized our layers to match the size of the image, so no scaling is necessary.

Last, our use of CATransaction is notable. CATransaction enables us to group several changes to be executed at once by surrounding them in calls to [CATransaction begin] and [CATransaction commit]. Try commenting out the surrounding begin/commit calls. You may notice some flickering in the display as the layers are shown before the animation begins.

CATransaction has methods to affect changes on the actions within the current transaction, such as changing the duration, timing functions, or perhaps most useful, disabling all actions, which turns off animations:

[CATransaction setDisableActions:YES];

You may have noticed that Scattered presently limits the number of

Return Main Page Previous Page Next Page

®Online Book Reader