2011-04-13 29 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座標存儲在NSArray中。然後在drawRect:只是循環通過NSBezierPath對象的數組並繪製每個對象。

+0

非常感謝。這聽起來像它會工作 – Justin 2011-04-14 17:42:54

+0

它完美的工作!非常感謝! – Justin 2011-04-15 13:48:58

相關問題