Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change animation time for properties of a CALayer

I have a CALayer to animate a change in its image contents. Now, how can I change how long it takes for this animation to take place?

like image 450
Alexsander Akers Avatar asked May 28 '10 02:05

Alexsander Akers


3 Answers

You can just call:

[CATransaction setAnimationDuration:durationSecs] 

in -layoutSublayers or anywhere else that you modify the layers and expect them to implicitly animate. This will effect the current implicit transaction and any sub-transactions within this one.

like image 102
Ben Lachman Avatar answered Oct 04 '22 06:10

Ben Lachman


A different way to do this:

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:2.5f] forKey:kCATransactionAnimationDuration];
//Perform CALayer actions, such as changing the layer contents, position, whatever.
aCALayerObject.contents = [self newCALayerContents];    
[CATransaction commit];

That code would animate the change of the CALayer's contents over 2.5 seconds. You can also use this to disable all animations completely. Like this:

[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
like image 25
Charliehorse Avatar answered Oct 04 '22 06:10

Charliehorse


It's more or less simple. You have an ivar CALayer *yourLayer. Then you set the delegate and implement the delegate method -(id<CAAction>)actionForLayer:forKey:

- (void)awakeFromNib {
    yourLayer.delegate = self;
    yourLayer.name = @"yourLayer";
}  
- (id <CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event {
    if([layer.name isEqualToString yourLayer.name]) { // Check for right layer

        CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:event]; // Default Animation for 'event'
        ani.duration = .5; // Your custom animation duration
        return ani;

    } else return nil; // Default Animation
}
like image 30
papr Avatar answered Oct 04 '22 05:10

papr