osx - How to display animated GIF in Objective C on top of the layered View? -
i trying draw animated gif on screen in mac osx app . used code insert gif: can see gif 1 picture doesn't animates static picture :( should add make 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]; // ********** 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 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 1 - (void)insertgif1 { [imageview removefromsuperview]; [imageview setimagescaling:nsimagescalenone]; [imageview setanimates: yes]; imageview.image = [nsimage imagenamed:@"fanblades11.gif"]; [self addsubview:imageview]; } @end
edit: discovered source of problem: adding class (that represents gif inside circle) on top of rmblurredview
, animations doesn't work when adding subview ,however works on other views added.
any ideas reason inside rmblurredview
stop nsimageview animating ?
edit: think [self setwantslayer:yes];
reason not getting animations how can still animation feature enabled?
edit:
here simple sample problem
"you must disable autoscaling feature of nsimageview animation playback function. after you've done that, no programming required. works charm!"
--http://www.cocoabuilder.com/archive/cocoa/108530-nsimageview-and-animated-gifs.html
imageview.imagescaling = nsimagescalenone; imageview.animates = yes;
needed layer backed views:
if image view in layer backed view or layer backed itself:
imageview.candrawsubviewsintolayer = yes;
working example using question's own gif:
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];
Comments
Post a Comment