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

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

问题描述

我正在尝试在 mac OSX 应用程序的屏幕上绘制动画 gif.我使用此代码插入 gif:我可以将 Gif 视为 1 张没有动画的图片只有静态图片 :( 我应该添加什么才能使它具有动画效果?

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

我发现了问题的根源:我在 RMBlurredView 顶部添加了我的类(代表圆圈内的 gif)当我将其添加为子视图时动画不起作用,但它适用于我添加的所有其他视图.

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.

任何想法在 RMBlurredView 中阻止我的 NSImageView 动画的原因是什么?

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

我认为 [self setWantsLayer:YES]; 是我没有得到动画的原因启用此功能后,我如何才能获得动画?

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

这是我的问题的简单示例

Here is a simple sample with my problem

http://snk.to/f-cdk3wmfn

我的 gif:这是我的 gif,它在白色背景色下不可见

推荐答案

"您必须禁用 NSImageView 的自动缩放功能动画播放功能.完成后,没有额外的需要编程.它就像一个魅力!"

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

<小时>

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


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

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

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