2011-04-13 3 views
0

나는 이미지 편집기에 완벽한 출발이라고 생각한 것을 만들었습니다. 클릭 할 때마다 정사각형이 그려집니다. 내 문제는 다른 장소를 클릭하거나 마우스를 끌 때 사각형을 이동한다는 것입니다 (다른 사각형을 그리는 대신). 현재의 내용을 어디서나 "드래그"하지 않고 어떻게 커스텀 뷰를 그릴 수 있습니까?NSImageWell에 사용자 정의보기를 넣는 방법?

헤더 (.H) (가 호출 무엇이든)

NSBezierPath *thePath; 
NSColor *theColor; 
NSTimer *updateTimer; 
NSPoint *mousePoint; 
int testInt = 1; 
int x = 0; 
int y = 0; 

@interface test : NSView { 
    IBOutlet NSView *myView; 
    IBOutlet NSButton *button; 

} 

@property (readwrite) NSPoint mousePoint; 

@end 

하는 .m 파일

@implementation test 

@synthesize mousePoint; 

- (void) mouseDown:(NSEvent*)someEvent {   
    mousePoint = [someEvent locationInWindow]; 
    NSLog(@"Location: x= %f, y = %f", (float)mousePoint.x, (float)mousePoint.y); 
    x = mousePoint.x; 
    y = mousePoint.y; 
    [button setHidden:TRUE]; 
    [button setHidden:FALSE]; 
    [self setNeedsDisplay:YES]; 

} 

- (void) mouseDragged:(NSEvent *)someEvent { 
    mousePoint = [someEvent locationInWindow]; 
    NSLog(@"Location: x= %f, y = %f", (float)mousePoint.x, (float)mousePoint.y); 
    x = mousePoint.x; 
    y = mousePoint.y; 
    [button setHidden:TRUE]; 
    [button setHidden:FALSE]; 
    [self setNeedsDisplay:YES]; 

} 

- (void) drawRect:(NSRect)rect; { 
    thePath = [NSBezierPath bezierPathWithRect:NSMakeRect(x, y, 10, 10)]; 
    theColor = [NSColor blackColor]; 
    [theColor set]; 
    [thePath fill]; 

} 

@end 

하지 않는 이유는이 작품 : 여기에

내 코드?

답변

1

현재 직사각형이 주변에있는 유일한 직사각형이기 때문에 이동합니다. 여러 개의 사각형을 그려서 계속 유지하려면 사각형을 나타내는 NSBezierPath 개체를 어딘가에 저장해야합니다 (나는 NSArray을 제안합니다). 그런 다음 배열을 반복하고 각각을 그립니다.

현재 코드를보고 mouseUp:을 구현하고 x 및 y 좌표가 NSArrayNSBezierPath 개체를 저장해야합니다. 그럼 drawRect: 그냥 NSBezierPath 배열을 통해 루프와 각각을 그립니다.

+0

고맙습니다. 그게 작동 할 것 같네요 – Justin

+0

완벽하게 작동했습니다! 고마워요! – Justin

관련 문제