2014-10-03 4 views
1

RubyMotion을 사용하여 Quartz 2D programming guide을 따르려고합니다. 여기 RubyMotion : Mac 용 Quartz 2D 프로그래밍 가이드를 따르려고합니다

입니다 내 AppDelegate : 나는 다음과 같은 오류가 받고 있어요

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 

:

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입니다

<Error>: CGContextSetRGBFillColor: invalid context 0x10222bad0 
<Error>: CGContextFillRects: invalid context 0x10222bad0 
<Error>: CGContextSetRGBFillColor: invalid context 0x10222bad0 
<Error>: CGContextFillRects: invalid context 0x10222bad0 

왜 문맥이 잘못 되었습니까? 나는 drawRect 방법 안에 있어요. 내가 [[340, 380], [480, 360]]에 창 RECT을 변경하는 경우

편집 오류 멀리 간다, 그러나 drawRect가 호출되지 않습니다. 그러나 창 크기를 조정하면 동일한 오류가 발생합니다.

편집 2 이것은 OS X 앱입니다.

은 EDIT 3, 목표 - C에서 같은 프로그램은 잘 흥미로운 작품 :

myContext = NSGraphicsContext.currentContext.graphicsPort.to_object 
: 당신은 당신이 컨텍스트를 얻을 때 to_object를 호출 할 필요가 : 여기
// 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 
+0

2 개의 채워진 사각형을 작성하려고합니다. 서로 겹치지? –

+0

@ rahul_send89, 네,하지만 적어도 하나의 직사각형을 그릴 수 있다면 그건 획기적인 것이 될 것입니다. 이 가이드를 따르려고합니다. https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html – Halst

+0

같은 답변을 추가하는 경우 .. ' 엎드려. 작동하지 않으면 ping을 실행하십시오. 변경 또는 델. 괜찮 니? –

답변

1

가 작동하게하는 마술

나는 RubyMotion 프로젝트에 코드를 구현하고,이 결과였다 :

enter image description here

+0

고마워요.이'# to_object' 메소드는 어쨌든 무엇입니까? – Halst

+0

'to_object'에 대한 문서를 보지 못했습니다. 공식 OS X 코드 샘플을보고 알아 냈습니다. 특히'PathDemo' : https://github.com/HipByte/RubyMotionSamples/tree/ 마스터/osx – vacawama

관련 문제