How to display an animated GIF in Objective-C on top of a layered view?

I am trying to draw an animated gif on my screen on Mac OSX. I used this code to insert a gif: I see Gif as 1 image, which it doesnโ€™t enliven only the static picture :( what should I add to make it animated?

#import <Cocoa/Cocoa.h> #import <Quartz/Quartz.h>//for drawing circle #import "sharedPrefferences.h" @interface GenericFanSubView : NSView { NSColor * _backgroundColor; NSImageView* imageView; } - (void)setBackgroundColor :(NSColor*)color; - (void)insertGif1; - (void)insertGif2; - (void)insertGif3; @end #import "GenericFanSubView.h" #define PI 3.14285714285714 @implementation GenericFanSubView - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code here. imageView = [[NSImageView alloc]initWithFrame:CGRectMake(0, 0,self.frame.size.width,self.frame.size.height)]; [imageView setAnimates: YES]; } return self; } - (void)drawRect:(NSRect)dirtyRect { [super drawRect:dirtyRect]; // Drawing code here. [self drawCircleInRect]; _backgroundColor = [NSColor whiteColor]; [self insertGif1]; } -(void)drawCircleInRect { //draw colored circle here CGContextRef context = [[NSGraphicsContext // 1 currentContext] graphicsPort]; // ********** Your drawing code here ********** // 2 CGContextSetFillColorWithColor(context,[self NSColorToCGColor:(_backgroundColor)]); float radius1 = self.frame.size.height/2; float startAngle = 0; float endAngle = endAngle = PI*2; CGPoint position = CGPointMake(self.frame.size.height/2,self.frame.size.height/2);//center of the view CGContextBeginPath(context); CGContextAddArc(context, position.x, position.y, radius1, startAngle, endAngle, 1); CGContextDrawPath(context, kCGPathFill); // Or kCGPathFill } - (void)setBackgroundColor :(NSColor*)color { _backgroundColor = color; [self setNeedsDisplay:YES]; } - (CGColorRef)NSColorToCGColor:(NSColor *)color { NSInteger numberOfComponents = [color numberOfComponents]; CGFloat components[numberOfComponents]; CGColorSpaceRef colorSpace = [[color colorSpace] CGColorSpace]; [color getComponents:(CGFloat *)&components]; CGColorRef cgColor = CGColorCreate(colorSpace, components); return cgColor; } //curentlly calling only this 1 - (void)insertGif1 { [imageView removeFromSuperview]; [imageView setImageScaling:NSImageScaleNone]; [imageView setAnimates: YES]; imageView.image = [NSImage imageNamed:@"FanBlades11.gif"]; [self addSubview:imageView]; } @end 

Edit: I discovered the source of the problem: I added my class (representing the gif inside the circle) on top of RMBlurredView and the animation does not work when I add it as a subview, but it works with all the other views that I added.

Any ideas what could be the reason inside RMBlurredView to stop the NSImageView animation?

Edit: I think [self setWantsLayer:YES]; - the reason why I donโ€™t get animation, how can I get animation with the function turned on?

Edit:

Here is a simple example with my problem

http://snk.to/f-cdk3wmfn

my gif: This is my gif that is invisible on a white background

+6
source share
1 answer

"You must disable the NSImageView autoscale feature to play the animation. After you have done this, no additional programming is required. It works like a charm!"

- http://www.cocoabuilder.com/archive/cocoa/108530-nsimageview-and-animated-gifs.html

 imageView.imageScaling = NSImageScaleNone; imageView.animates = YES; 

required for view with layer support:

if the image view is in layer-supported mode or the layer itself is supported:

 imageView.canDrawSubviewsIntoLayer = YES; 

working example using native gif question:

 NSImageView *view = [[NSImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)]; view.imageScaling = NSImageScaleNone; view.animates = YES; view.image = [NSImage imageNamed:@"FanBlades2_42x42.gif"]; view.canDrawSubviewsIntoLayer = YES; NSView *layerview = [[NSView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)]; layerview.wantsLayer = YES; [layerview addSubview:view]; [self.window.contentView addSubview:layerview]; 
+15
source

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


All Articles