如何将自定义视图放入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
为什么不起作用?     
已邀请:
它会移动您当前的矩形,因为那是唯一的矩形。如果要绘制多个矩形并使它们保留,则需要将代表矩形的
NSBezierPath
对象存储在某个位置(我建议使用
NSArray
),然后遍历数组并绘制每个矩形。 查看当前代码,应实现
mouseUp:
并将x和y坐标的
NSBezierPath
对象存储在
NSArray
中。然后在
drawRect:
中循环遍历
NSBezierPath
个对象的数组并绘制每个对象。     

要回复问题请先登录注册