Tib*_*abo 4 cocoa core-animation calayer
我想在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];
}
Run Code Online (Sandbox Code Playgroud)
TL;博士:
通过调用使您的视图图层托管
[iconV setLayer:[CALayer layer]];
[iconV setWantsLayer:YES];
Run Code Online (Sandbox Code Playgroud)
没有发生任何事情的原因是您的图像视图没有图层,因此当您调用时,[iconV.layer addSublayer:layer];
您正在发送消息nil
并且没有任何反应(子图层未添加到图像视图中).
默认情况下,OS X上的视图不使用Core Animation层作为其后备存储,以实现向后兼容性.具有图层的视图可以是图层支持或图层托管.
您不应该直接与图层支持的视图交互,也不应该向图层托管视图添加视图(但可以添加图层).由于您要将图层添加到图像视图图层(从而直接与图层进行交互),因此您需要一个图层托管视图.
你可以告诉你的观点,它应该是图层托管,首先给它使用图层[iconV setLayer:[CALayer layer]];
,然后(顺序很重要)告诉它它想要一个图层使用[iconV setWantsLayer:YES];
.
有关图层支持和图层托管视图的更多信息,请阅读文档wantsLayer
.