如何在分层视图顶部的Objective C中显示动画GIF? [英] How to display animated GIF in Objective C on top of the layered View?

查看:345
本文介绍了如何在分层视图顶部的Objective C中显示动画GIF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Mac OSX应用程序中在我的屏幕上绘制动画的GIF。
我使用这个代码插入gif:我可以看到GIF作为1图片它不动画
只有静态图片(我应该添加什么使它动画?

  #import< Cocoa / Cocoa.h> 
#import< Quartz / Quartz.h> //绘图圈
#importsharedPrefferences.h
@interface GenericFanSubView:NSView
{
NSColor * _backgroundColor;
NSImageView * imageView;
}
$ b b - (void)insertGif2;
- (void)insertGif3; $ b $(b) b @end

#importGenericFanSubView.h
#define PI 3.14285714285714

@implementation GenericFanSubView

- (id)initWithFrame :(NSRect)frame
{
self = [super initWithFrame:frame];
if(self){
//这里初始化代码
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];
//这里绘制代码。
[self drawCircleInRect];
_backgroundColor = [NSColor whiteColor];
[self insertGif1];
}

- (void)drawCircleInRect
{
//在此绘制彩色圆圈
CGContextRef context = [[NSGraphicsContext // 1
currentContext] graphicsPort];
// **********你的绘图代码在这里********** // 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); //视图中心
CGContextBeginPath(context);
CGContextAddArc(context,position.x,position.y,radius1,startAngle,endAngle,1);
CGContextDrawPath(context,kCGPathFill); //或kCGPathFill
}
- (void)setBackgroundColor:(NSColor *)color
{
_backgroundColor = color;

[self setNeedsDisplay:YES];
}

- (CGColorRef)NSColorToCGColor:(NSColor *)color
{
NSInteger numberOfComponents = [color numberOfComponents];
CGFloat组件[numberOfComponents];
CGColorSpaceRef colorSpace = [[color colorSpace] CGColorSpace];
[color getComponents:(CGFloat *)& components];
CGColorRef cgColor = CGColorCreate(colorSpace,components);
return cgColor;
}

// curentlly只调用这个1
- (void)insertGif1
{
[imageView removeFromSuperview];

[imageView setImageScaling:NSImageScaleNone];

[imageView setAnimates:YES];

imageView.image = [NSImage imageNamed:@FanBlades11.gif];

[self addSubview:imageView];
}

@end

编辑:的问题:
我在 RMBlurredView
上添加我的类(代表圈内的gif),动画不工作我添加它作为子视图,但它适用于我添加的所有其他视图。



任何想法可能是 RMBlurredView <

编辑:
我认为 [self setWantsLayer:YES];

code>是我没有得到动画的原因
如何仍然可以使用此功能启用动画?



编辑:



这是一个简单的问题示例



http://snk.to/f-cdk3wmfn



我的gif:这是我的gif它在白色背景颜色上不可见

解决方案

您必须禁用NSImageView的自动缩放功能,才能使
动画播放正常运行。完成后,不需要额外的
编程。它的作用就像一个魅力!



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

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


$ b $如果图片视图位于图层视图中,或者图层视图位于图层视图中,或者图层视图位于图层视图的背面,则

  imageView.canDrawSubviewsIntoLayer = YES; 






工作示例使用问题自己的gif:

  NSImageView * view = [[NSImageView alloc] initWithFrame:CGRectMake(10,10,50,50)]; 
view.imageScaling = NSImageScaleNone;
view.animates = YES;
view.image = [NSImage imageNamed:@ [NSView alloc] initWithFrame:CGRectMake(0,0,60,60)];这是一个简单的例子。
layerview.wantsLayer = YES;
[layerview addSubview:view];

[self.window.contentView addSubview:layerview];


I am trying to draw animated gif on my screen in mac OSX app . I used this code to insert the gif: I can see the Gif as 1 picture it doesn't animates only 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 was adding my class (that represents gif inside the circle) on top of RMBlurredView and the animations doesn't work when I adding it as subview ,However it works on all the other views I added.

Any ideas what could be the reason inside the RMBlurredView to stop my NSImageView from animating ?

Edit: I think [self setWantsLayer:YES]; is the reason I am not getting animations how can I still get the animation with this feature enabled?

Edit:

Here is a simple sample with my problem

http://snk.to/f-cdk3wmfn

my gif:This is my gif it is invisible on white background color

解决方案

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

这篇关于如何在分层视图顶部的Objective C中显示动画GIF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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