【发布时间】:2015-02-22 02:21:29
【问题描述】:
我正在尝试制作一个简单的应用程序,其中我有一个侧边栏,并且我正在尝试根据 PSD 文件中的 RGB 值获取背景颜色,或者使用背景图像作为图案......
我在这两种方式上都进行了尝试,但到目前为止没有任何效果。任何帮助将不胜感激。
-(void) drawRect:(NSRect)dirtyRect {
CALayer *viewLayer = [CALayer layer];
[viewLayer setBackgroundColor:CGColorCreateGenericRGB(85.0, 179.0, 217.0, 1.0)]; //RGB plus Alpha Channel
[self setWantsLayer:YES]; // view's backing store is using a Core Animation Layer
[self setLayer:viewLayer];
}
这段代码应该是蓝色的,结果几乎是白色的......甚至不接近我想要的。
第二个代码,显示黑色背景,即使我的png文件在支持文件的文件夹中。
- (void)drawRect:(NSRect)dirtyRect {
NSGraphicsContext* theContext = [NSGraphicsContext currentContext];
[theContext saveGraphicsState];
[[NSGraphicsContext currentContext] setPatternPhase:NSMakePoint(0,[self frame].size.height)];
[self.customBackgroundColour set];
NSRectFill([self bounds]);
[theContext restoreGraphicsState];
}
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.customBackgroundColour = [NSColor colorWithPatternImage:
[NSImage imageNamed:@"buttonBg.png"]];
}
return self;
}
再次感谢任何帮助。
【问题讨论】:
-
您确定您的
-initWithFrame:正在被调用吗?此外,您通常不应更改-drawRect:中的视图状态(例如wantsLayer和layer属性)。是用来画画的。状态应在此之前建立;最迟在-viewWillDraw。
标签: objective-c macos cocoa