2012-04-28 4 views
0

버튼 클릭 후 특정 창에 선을 그릴 수 있습니까?코코아에 라인을 그립니까?

내가이 사용하고 있습니다 :

NSBezierPath * path = [NSBezierPath bezierPath]; 
     [path setLineWidth: 4]; 

     NSPoint startPoint = { 21, 21 }; 
     NSPoint endPoint = { 128,128 }; 
     [path moveToPoint: startPoint];  
     [path lineToPoint:endPoint]; 

     [[NSColor redColor] set]; 
     [path stroke]; 

을하지만 난에 넣어 경우에만 작동 : 내가이 문제를 해결하는 방법을

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 

? 내 목표는 세부 사항 (좌표)에 따라 선을 그릴 수있는 응용 프로그램을 만드는 것입니다

감사합니다.

+0

어디에서 (어떤보기에서) 그려야합니까? NSView의 하위 클래스를 만들고 거기에 드로잉 코드를 넣어야한다고 생각합니다. 왜 당신이 applicationWillFinishLaunching 메서드에 넣을 때 서브 클래 싱없이 작동하는지 잘 모르겠습니다. – rdelmar

+0

좋아, NSview 클래스 내부 및 사용자 정의보기 내부 드로잉 해요. – Corninos

답변

0

그림을 완성한 시점에 [yourView setNeedsDisplay : YES]를 호출 해 보았습니까?

2

보기 또는 레이어의 그리기 방법 (예 : drawRect:) 외부로 그려서는 안됩니다. 넓은 스트로크에서 원하는 것은 플래그가 설정된 경우 선을 그려주는보기가 있고 버튼을 클릭하면 플래그를 설정하고 다시 그리기를 지시합니다.

0

마우스 이벤트를 클릭하면. 이 코드는 선, 곡선 및 그리기를 생성합니다.

#import <Cocoa/Cocoa.h> 


    @interface BezierView : NSView { 
     NSPoint points[4]; 
     NSUInteger pointCount; 
    } 

    @end 

    #import "BezierView.h" 

    @implementation BezierView 
    - (id)initWithFrame:(NSRect)frame { 
     self = [super initWithFrame:frame]; 
     if (self) { 
      // Initialization code here. 
     } 
     return self; 
    } 

    - (void)drawRect:(NSRect)rect 
    { 
     NSBezierPath *control1 = [NSBezierPath bezierPath]; 
     [control1 moveToPoint: points[0]]; 
     [control1 lineToPoint: points[1]]; 
     [[NSColor redColor] setStroke]; 
     [control1 setLineWidth: 2]; 
     [control1 stroke]; 

     NSBezierPath *control2 = [NSBezierPath bezierPath]; 
     [control2 moveToPoint: points[2]]; 
     [control2 lineToPoint: points[3]]; 
     [[NSColor greenColor] setStroke]; 
     [control2 setLineWidth: 2]; 
     [control2 stroke]; 

     NSBezierPath *curve = [NSBezierPath bezierPath]; 
     [curve moveToPoint: points[0]]; 
     [curve curveToPoint: points[3] 
       controlPoint1: points[1] 
       controlPoint2: points[2]]; 

     [[NSColor blackColor] setStroke]; 
     CGFloat pattern[] = {4, 2, 1, 2}; 
     [curve setLineDash: pattern 
        count: 4 
        phase: 1]; 
     [[NSColor grayColor] setFill]; 
     [curve fill]; 
     [curve stroke]; 
    } 

    - (void)mouseDown: (NSEvent*)theEvent 
    { 
     NSPoint click = [self convertPoint: [theEvent locationInWindow] 
            fromView: nil]; 
     points[pointCount++ % 4] = click; 
     if (pointCount % 4 == 0) 
     { 
      [self setNeedsDisplay: YES]; 
     } 
    } 
    @end