【发布时间】: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]; 是我没有得到动画的原因
在启用此功能的情况下,我怎样才能获得动画?
编辑:
这是我的问题的一个简单示例
我的 gif:This is my gif it is invisible on white background color
【问题讨论】:
标签: objective-c macos cocoa animated-gif nsimage