【问题标题】:How to display animated GIF in Objective C on top of the layered View?如何在分层视图之上的 Objective C 中显示动画 GIF?
【发布时间】:2014-07-21 02:28:30
【问题描述】:

我正在尝试在 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, 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) 并且当我将其添加为子视图时动画不起作用,但是它适用于我添加的所有其他视图。

知道RMBlurredView 中阻止我的 NSImageView 动画的原因是什么?

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

编辑:

这是我的问题的一个简单示例

http://snk.to/f-cdk3wmfn

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

【问题讨论】:

    标签: objective-c macos cocoa animated-gif nsimage


    【解决方案1】:

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

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

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

    需要图层支持的视图:

    如果图像视图在 layer backed view 或者 layer backeded 本身:

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

    【讨论】:

    • 试过这个[imageView setImageScaling:NSImageScaleNone];没有帮助:(
    • 我设置的是,仍然是静态的 :(
    • 当我在 xCode 中点击它时,它的动画效果很好。
    • 在预览应用程序中它没有动画,但我看到左侧的所有帧。
    • 你现在有超过 10 个代表 :)
    猜你喜欢
    • 2017-10-03
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 2011-08-11
    相关资源
    最近更新 更多