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

 小菜鸡 发布于 2023-01-12 12:55

我想在mac OSX应用程序中在我的屏幕上绘制动画gif.我使用这段代码插入gif:我可以看到Gif为1张图片它不会动画只有静态图片:(我应该添加什么才能使其动画?

#import 
#import //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

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

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

编辑:我认为这[self setWantsLayer:YES];是我没有动画的原因我怎么能在启用此功能的情况下获得动画?

编辑:

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

http://snk.to/f-cdk3wmfn

我的GIF:这是我的GIF,它在白色背景颜色上是看不见的

1 个回答
  • "你必须禁用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, 0, 60, 60)];
    layerview.wantsLayer = YES;
    [layerview addSubview:view];
    
    [self.window.contentView addSubview:layerview];
    

    2023-01-12 12:58 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有