更改CALayer属性的动画时间

时间:2010-05-28 02:41:10

标签: cocoa core-animation quartz-graphics

我有一个CALayer来动画其图像内容的变化。现在,我该如何改变这个动画发生的时间?

3 个答案:

答案 0 :(得分:19)

您可以致电:

[CATransaction setAnimationDuration:durationSecs] 

在-layoutSublayers或您修改图层的任何其他位置,并期望它们隐式动画。这将影响当前隐式事务和此事务中的任何子事务。

答案 1 :(得分:10)

另一种方法:

[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];

该代码将在2.5秒内激活CALayer内容的更改。您也可以使用它来完全禁用所有动画。像这样:

[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];

答案 2 :(得分:7)

这或多或少都很简单。你有一个ivar CALayer *yourLayer。然后设置委托并实现委托方法-(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
}