我正在嘗試使用RubyMotion關注Quartz 2D programming guide。RubyMotion:試圖遵循Mac的石英2D編程指南
這裏是我的AppDelegate
:
class AppDelegate
def applicationDidFinishLaunching(notification)
buildMenu
buildWindow
end
def buildWindow
@window = NSWindow.alloc.initWithContentRect([[240, 180], [480, 360]],
styleMask: NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask,
backing: NSBackingStoreBuffered,
defer: false)
@window.title = NSBundle.mainBundle.infoDictionary['CFBundleName']
@window.orderFrontRegardless
@view = MyQuartzView.alloc.initWithFrame(@window.frame)
@window.contentView.addSubview @view
end
end
這裏是我的MyQuartzView
,這應該是從代碼的指南中直接翻譯:
class MyQuartzView < NSView
def drawRect(rect)
myContext = NSGraphicsContext.currentContext.graphicsPort
CGContextSetRGBFillColor(myContext, 1, 0, 0, 1)
CGContextFillRect(myContext, CGRectMake(0, 0, 200, 100))
CGContextSetRGBFillColor(myContext, 0, 0, 1, 0.5)
CGContextFillRect(myContext, CGRectMake(0, 0, 100, 200))
end
end
我收到以下錯誤:
<Error>: CGContextSetRGBFillColor: invalid context 0x10222bad0
<Error>: CGContextFillRects: invalid context 0x10222bad0
<Error>: CGContextSetRGBFillColor: invalid context 0x10222bad0
<Error>: CGContextFillRects: invalid context 0x10222bad0
爲什麼上下文無效?我在drawRect
方法裏面。
編輯 如果我改變窗口RECT到[[340, 380], [480, 360]]
錯誤消失,但drawRect
不叫。但是,當我調整窗口大小時,會調用相同的錯誤。
編輯2 這是一個OS X應用程序。
編輯3有趣的是,在Objective-C相同的程序工作正常:
// main.m
#import <Cocoa/Cocoa.h>
#import "MyQuartzView.h"
int main(int argc, const char * argv[])
{
NSApplication *app = [NSApplication sharedApplication];
NSRect frame = NSMakeRect(100., 100., 300., 300.);
NSWindow *window = [[NSWindow alloc]
initWithContentRect: frame
styleMask: NSTitledWindowMask | NSClosableWindowMask
backing: NSBackingStoreBuffered
defer: false];
[window setTitle: @"Test"];
id view = [[MyQuartzView alloc] initWithFrame: frame];
[window setContentView: view];
[window setDelegate: view];
[window orderFrontRegardless];
[app run];
return EXIT_SUCCESS;
}
// MyQuartzView.m
#import "MyQuartzView.h"
@implementation MyQuartzView
- (id)initWithFrame:(NSRect)frame
{
return[super initWithFrame:frame];
}
- (void)drawRect:(NSRect)dirtyRect
{
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetRGBFillColor (myContext, 1, 0, 0, 1);
CGContextFillRect (myContext, CGRectMake (0, 0, 200, 100));
CGContextSetRGBFillColor (myContext, 0, 0, 1, .5);
CGContextFillRect (myContext, CGRectMake (0, 0, 100, 200));
}
@end
是你打算創建2填充矩形。哪些相互重疊? – 2014-10-06 09:29:11
@ rahul_send89,是的,但是如果我至少可以繪製一個矩形 - 那將是一個突破。我試圖按照這個指南:https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html – Halst 2014-10-06 09:37:46
如果我添加一個答案相同..請不要' t投票下來。 如果它不工作,就會ping我。會改變或刪除。聽起來不錯? – 2014-10-06 09:41:46