【问题标题】:How to animate a png sequence with core animation in cocoa (not touch)如何在可可中使用核心动画为 png 序列设置动画(非触摸)
【发布时间】:2012-10-22 12:30:59
【问题描述】:

我想在 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];
}

【问题讨论】:

  • 发生了什么?没有显示或仅显示第一张图像? (这可能是一个愚蠢的问题)你的图像视图层支持吗? (你有没有打电话给 [iconV setWantsLayer:YES];?)
  • 现在,它可以工作了!那很简单。我没有使用 [iconV setWantsLayer:YES] 我不知道。请把你的评论作为答案,我会接受的!非常感谢!

标签: cocoa core-animation calayer


【解决方案1】:
tl;博士:

通过调用使您的视图层托管

[iconV setLayer:[CALayer layer]];
[iconV setWantsLayer:YES];

为什么什么都没发生

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

为了向后兼容,OS X 上的视图默认不使用 Core Animation 层作为它们的后备存储。带有层的视图可以是 layer-backedlayer-hosting

您不应该直接与 layer-backed 视图进行交互,也不应该将 views(但可以添加图层)添加到 layer-hosting 视图。由于您将图层添加到图像视图图层(并因此直接与其交互),因此您需要一个图层托管视图。

修复它

你可以告诉你的视图它应该是层托管的,首先使用[iconV setLayer:[CALayer layer]];给它层,然后(顺序很重要)告诉它它需要一个层使用[iconV setWantsLayer:YES];

有关 layer-backed 和 layer-hosting 视图的更多信息,请阅读the documentation for wantsLayer

【讨论】:

  • 我认为如果你想操作一个视图的层,那么层必须明确分配,例如view.layer = [CALayer layer];setWantsLayer: 告诉视图为自己的用途创建一个层。 developer.apple.com/library/mac/documentation/Cocoa/Reference/…:
  • 是的。这使它成为一个图层托管视图。 (在文档中甚至是粗体)..我会更新我的答案。
【解决方案2】:

我迟到了,但我会发布我的解决方案,因为原来的一个加上@David 发布的答案对我不起作用。我在 Xcode 8.3.3 上创建,为 10.10 编译。

对我来说不起作用的是单独创建图层,然后用它设置图像视图。

这对我不起作用

CALayer *layer = [CALayer layer];
layer.frame = self.imageView.bounds;
layer.bounds = self.imageView.bounds;
[self.imageView setLayer:layer];

结果总是没有动画。

显然,在 10.10,当你这样做时:

self.imageView.wantsLayer = YES;

MacOS 创建一个图层并将其分配给图像视图。

所以,对我有用的代码是:

self.imageView.wantsLayer = YES;

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
[animation setCalculationMode:kCAAnimationDiscrete];
[animation setDuration:1.3f];
[animation setRepeatCount:HUGE_VALF];
[animation setValues:imageArray];
[self.imageView.layer addAnimation:animation forKey:@"contents"];  
[self.imageView setAnimates:YES];

[self.imageView setCanDrawSubviewsIntoLayer:YES];

最后一行非常重要。如果您删除它,它将不会显示任何图像!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多