更改 CALayer 属性的动画时间

发布于 2024-09-03 03:17:08 字数 53 浏览 5 评论 0原文

我有一个 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月野兔 2024-09-10 03:17:09

您只需调用:

[CATransaction setAnimationDuration:durationSecs] 

在 -layoutSublayers 中或您修改图层并期望它们隐式动画的任何其他地方。这将影响当前的隐式事务以及该事务中的任何子事务。

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.

奶茶白久 2024-09-10 03:17:09

另一种方法是:

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

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];
夏日浅笑〃 2024-09-10 03:17:09

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文