如何使用可可中的核心动画来动画化一个png序列(非触摸) [英] How to animate a png sequence with core animation in cocoa (not touch)

查看:108
本文介绍了如何使用可可中的核心动画来动画化一个png序列(非触摸)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在NSImageView中对一个png序列进行动画处理,但是我不能使它工作。它只是不想显示任何动画。任何建议?



这是我的代码:

   - )imageAnimation {
NSMutableArray * iconImages = [[NSMutableArray allocate] 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];

//添加到NSImageView图层
[iconV.layer addSublayer:layer];
}


解决方案

tl; dr: strong>

通过调用

使您的视图层托管。

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






为什么不发生

$ b $没有什么发生的原因是你的图像视图没有一层,所以当你调用 [iconV.layer addSublayer:layer]; 您正在向 nil 发送消息,并且什么也不会发生(子图层不会添加到图像视图)。



OS X上的视图默认情况下不使用Core Animation图层作为其后备存储,以实现向后兼容性。具有图层的视图可以是层支持的或层托管



您不应该直接与层次支持的视图进行交互,而且不应添加视图层是确定)到一个layer- 托管视图。由于您要将图层添加到图片视图图层(从而直接与其进行交互),因此您需要一个图层托管视图。



修复



你可以告诉你的观点,它应该是layer-hosting通过首先给它的图层使用 [iconV setLayer:[CALayer layer]]; 然后(顺序很重要)告诉它它想要一个图层使用 [iconV setWantsLayer:YES];



有关图层支持和图层托管视图的详情,请参阅 <$ c $的文档c> wantsLayer


I would like to animate a png sequence in a NSImageView, but I cannot make it work. It just does not want to show any animation. Any suggestion?

This is my code:

- (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;dr:

Make your view layer-hosting by calling

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


Why nothing happens

The reason nothing is happening is that your image view doesn't have a layer so when you call [iconV.layer addSublayer:layer]; you are sending a message to nil and nothing happens (the sublayer is not added to the image view).

Views on OS X don't use Core Animation layers as their backing store by default for backwards compatibility. A view with a layer can either be layer-backed or layer-hosting.

You should not interact directly with a layer-backed view and you shouldn't add views (but adding layers is ok) to a layer-hosting view. Since you are adding the layer to your image views layer (and thus interacting directly with it) you want a layer-hosting view.

Fixing it

You can tell your view that it should be layer-hosting by first giving it the layer using [iconV setLayer:[CALayer layer]]; and then (order is important) telling it that it wants a layer using [iconV setWantsLayer:YES];.

For more information on layer-backed and layer-hosting views please read the documentation for wantsLayer.

这篇关于如何使用可可中的核心动画来动画化一个png序列(非触摸)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆