gpt4 book ai didi

cocoa - 如何在 cocoa 中使用核心动画对 png 序列进行动画处理(非触摸)

转载 作者:行者123 更新时间:2023-12-03 16:13:46 30 4
gpt4 key购买 nike

我想在 NSImageView 中为 png 序列设置动画,但我无法使其工作。它只是不想显示任何动画。有什么建议吗?

这是我的代码:

- (void) imageAnimation {
NSMutableArray *iconImages = [[NSMutableArray alloc] init];
for (int i=0; i<=159; i++) {
NSString *imagePath = [NSString stringWithFormat:@"%@_%05d",@"clear",i];
[iconImages addObject:(id)[NSImage imageNamed:imagePath]];
//NSImage *iconImage = [NSImage imageNamed:imagePath];
//[iconImages addObject:(__bridge id)CGImageCreateWithNSImage(iconImage)];
}


CALayer *layer = [CALayer layer];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
[animation setCalculationMode:kCAAnimationDiscrete];
[animation setDuration:10.0f];
[animation setRepeatCount:HUGE_VALF];
[animation setValues:iconImages];

[layer setFrame:NSMakeRect(0, 0, 104, 104)];
layer.bounds = NSMakeRect(0, 0, 104, 104);
[layer addAnimation:animation forKey:@"contents"];

//Add to the NSImageView layer
[iconV.layer addSublayer:layer];
}

最佳答案

tl;博士:

通过调用使您的 View 层托管

[iconV setLayer:[CALayer layer]];
[iconV setWantsLayer:YES];
<小时/>

为什么什么也没发生

没有发生任何事情的原因是您的 ImageView 没有图层,因此当您调用 [iconV.layer addSublayer:layer]; 时您正在发送消息至nil并且没有任何反应(子层未添加到 ImageView 中)。

默认情况下,OS X 上的 View 不使用核心动画层作为后备存储以实现向后兼容性。带有图层的 View 可以是图层支持图层托管

您不应直接与图层支持的 View 交互,也不应向图层托管添加 View (但添加图层是可以的) View 。由于您要将图层添加到 ImageView 图层(从而直接与其交互),因此您需要一个图层托管 View 。

修复它

您可以通过首先使用 [iconV setLayer:[CALayer layer]]; 为其提供图层来告诉您的 View 它应该是图层托管的然后(顺序很重要)告诉它它想要一个使用 [iconV setWantsLayer:YES]; 的图层.

有关层支持和层托管 View 的更多信息,请阅读 the documentation for wantsLayer .

关于cocoa - 如何在 cocoa 中使用核心动画对 png 序列进行动画处理(非触摸),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13011527/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com