Change animation time for properties of a CALayer

烈酒焚心 提交于 2019-12-03 00:21:13
papr

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
}

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.

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!