objective-c – 如何在分层视图顶部的Objective C中显示动画GIF?

前端之家收集整理的这篇文章主要介绍了objective-c – 如何在分层视图顶部的Objective C中显示动画GIF?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在mac OSX应用程序中在我的屏幕上绘制动画gif.
我使用此代码插入gif:我可以看到Gif为1图片,它没有动画
只有静态图片:(我应该添加什么才能使其动画?
#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,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)
当我将其添加为子视图时,动画不起作用,但它适用于我添加的所有其他视图.

任何想法可能是RMBIurredView内部阻止我的NSImageView动画的原因?

编辑:
我想[self setWantsLayer:YES];这是我没有动画的原因
如何启用此功能仍然可以获得动画?

编辑:

以下是我的问题的简单示例

http://snk.to/f-cdk3wmfn

我的GIF:This is my gif it is invisible on white background color

解决方法

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

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

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

支持视图所需:

如果图像视图位于图层视图中,或者是图层视图本身:

imageView.canDrawSubviewsIntoLayer = YES;

使用问题自带的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,60,60)];
layerview.wantsLayer = YES;
[layerview addSubview:view];

[self.window.contentView addSubview:layerview];

猜你在找的C&C++相关文章