How to display animated GIF in Objective C on top of the layered View?

我的未来我决定 提交于 2019-11-30 05:24:49

"You must disable the autoscaling feature of the NSImageView for the animation playback to function. After you've done that, no extra programming required. It works like a charm!"

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

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

needed for layer backed views:

if the image view is in a layer backed view or is layer backed itself:

imageView.canDrawSubviewsIntoLayer = YES;

working example using the 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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!