How to animate the sequence of png with the main animation in cocoa (do not touch)

I would like to animate the png sequence in NSImageView, but I cannot get it to work. He just doesnโ€™t want to show the 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]; } 
+4
source share
2 answers
TL; dr:

Make your layer level preview by calling

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

Why nothing happens

The reason is that your image does not have a layer, so when you call [iconV.layer addSublayer:layer]; you send a message to nil and nothing happens (a sublevel is not added to the image view).

OS X views do not use core animation layers as the default repository for backward compatibility. A view with a layer can be either with support for layers or with a layer.

You cannot directly interact with a layer-supported view, and you should not add views (but add levels in order) to a layer-level view. Since you are adding a layer to the image viewer layer (and thus interacting directly with it), you need a layout view at the layer level.

Fixation

You can say that it should be a hosting layer, first giving it a layer using [iconV setLayer:[CALayer layer]]; and then (order is important), telling him that he wants to use the layer with [iconV setWantsLayer:YES]; .

For more information about layer-supported views and the layout level, please read the documentation for wantsLayer .

+5
source

I'm late to the party, but I will send my decision because the original plus answer sent by @David did not work for me. I am building on Xcode 8.3.3, compiling for 10.10.

What did not work for me was to create a layer separately and then set it with it.

it did not work for me

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

As a result, there was always no animation.

Apparently 10.10 when you do this:

 self.imageView.wantsLayer = YES; 

MacOS creates a layer and assigns it to the image view.

So, the code that worked for me was:

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

This last line is very important. If you delete this, it will not show any image!

0
source

Source: https://habr.com/ru/post/1441283/


All Articles