페이지

2012년 11월 10일 토요일

Core Animation Programming Summary 2


Animation


The animation occurs automatically in a separate thread, without further interaction with your application.

Animation Classes and Timing

- CABasicAnimation
- CAKeyframeAnimation
- CATransition
- CAAnimationGroup

Implicit Animation

Implicitly animating multiple properties of multiple layers
theLayer.opacity = 0.0;
theLayer.zPosition = -100;


Explicit Animation


ex 1 
CABasicAnimation *theAnimation;
theAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
theAnimation.duration = 3.0;
theAnimation.repeatCount = 2;
theAnimation.autoreverse = YES;
theAnimation.fromValue = [NSNumber numberWithFloat:1.0];
theAnimation.toValue = [NSNumber numberWithFloat:0.0];
[theLayer addAnimation:theAnimation forKey:@"animateOpacity"];

ex 2

CIFileter *filter = [CIFilter filterWithName:@"CIBloom"];
[filter setDefaults];
[filter setValue:[NSNumber numberWithFloat:5.0] forKey:@"inputRadius"];
[filter setName:@"pulseFilter"];
[selectionLayer setFilters:[NSArray arrayWithObject:filter]];
CABasicAnimation* pulseAnimation = [CABasicAnimation animation];
pulseAnimation.keyPath = @"filters.pulseFilter.inputIntensity";
pulseAnimation.fromValue = [NSNumber numberWithFloat:0.0];
pulseAnimation.toValue = [NSNumber numberWithFloat:1.5];
pulseAnimation.duration = 1.0;
pulseAnimation.repeatCount = HUGE_VALF;
pulseAnimation.autoreverses = YES;
pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[selectionLayer addAnimation:pulseAnimation forKey:@"pulseAnimation"];


Starting and Stopping Explicit Animations

addAnimation:forKey:
removeAnimationForKey:
removeAllAnimations


Layer Actions


A Layer is responsible for mapping action identifiers to the appropriate action objects.
typically, action objects are an instance of a CAAnimation subclass, which implements the CAAction protocol.
When an instance of CAAnimation receives the runActionForKey:object:arguments: message it responds by adding itself to the layer's animations

The Role of Action Objects
?
Defined Search Pattern for Action Keys
?
Adopting the CAAction Protocol
?
- (void) runActionForKey:(NSString*)key
object:(id)anObject
arguments:(NSDictionary*)dict
{
[(CALayer*)anObject addAnimation:self forKey:key];
}


Overriding an Implied Animation


- (id<CAAction>)actionForLayer:(CALayer*)theLayer
forKey:(NSString*)theKey
{
CATransition *theAnimation = nil;
if ([theKey isEqualToString:@"contents"])
{
theAnimation = [[CATransition alloc] init];
theAnimation.duration = 1.0;
theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
theAnimation.type = kCATransitionPush;
theAnimation.subtype = kCATransitionFromRight;
}
return theAnimation;
}



Implied animation for the sublayers property


NSMutableDictionary *customActions = [NSMutableDictionary dictionaryWithDictionary:[theLayer actions]];
//diable the animation for the sublayers property
[customActions setObject:[NSNull null] forKey:@"sublayers"];
theLayer.actions = customActions;



Transactions


Implicit transactions

theLayer.opacity = 0.0;
theLayer.zPosition = -200;
theLayer.position = CGPointMake(0.0, 0.0);

Explicit transactions

Explicit transactions are particularly useful when setting the properties of many layers at the same time (for example, while laying out multiple layers)

Temporarily disabling a layer's actions

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
[aLayer removeFromSuperlayer];
[CATranscation commit];



Overriding the animation duration

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:10.0f]
forKey:kCATransactionAnimationDuration];
theLayer.zPosition = 200.0;
theLayer.opacity = 0.0;
[CATransaction commit];


Nesting explicit transactions

[CATransaction begin]; //outer transaction
//change the animation duration to 2 secs
[CATransaction setValue:[NSNumber numberWithFloat:2.0f]
forKey:kCATransactionAnimationDuration];
theLayer.position = CGPointMake(0.0, 0.0);
[CATransaction begin]; //inner transaction
[CATransaction setValue:[NSNumber numberWithFloat:5.0f]
forKey:kCATransactionAnimationDuration];
theLayer.zPosition = 200.0;
theLayer.opacity = 0.0;
[CATransaction commit]; //inner transaction
[CATransaction commit]; //outer transaction



Laying Out Core Animation Layers

Contraints Layout Manager

Default Value Support

+(id)defaultValueForKey:(NSString*) key
{
if ( [key isEqualToString:@"masksToBounds"] )
return [NSNumber numberWithBool:YES];
return [super defaultValueForKey:key];
}



myLayer.transform.rotation.x = 0; // this is wrong
[myLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"transform.rotation.x"];
//this is right.

댓글 없음:

댓글 쓰기