以下代码应该在mapWindow NSView上绘制一个矩形。我的程序还有另一个使用NSView窗口的文件;因此我想要一个新窗口。但是,矩形不显示。任何帮助将不胜感激。
@interface mapWindow : NSView {@private NSView* theMapWindow;}
- (void)drawRect:(int)pointx: (int)pointy;
@property (assign) IBOutlet NSView* theMapWindow;
@end
@implementation mapWindow
@synthesize theMapWindow;
- (void)mouseDown:(NSEvent *)event
{
NSPoint point = [event locationInWindow];
//NSLog( @"mouseDown location: (%f,%f)", (float) point.x, (float) point.y);
[self drawRect:point.x:point.y];
}
- (void)drawRect:(int)pointx: (int)pointy
{
NSLog(@"Drawing point at (%d, %d)",pointx, pointy);
NSPoint origin = { pointx,pointy };
NSRect rect;
rect.origin = origin;
rect.size.width = 128;
rect.size.height = 128;
NSBezierPath * path;
path = [NSBezierPath bezierPathWithRect:rect];
[path setLineWidth:4];
[[NSColor whiteColor] set];
[path fill];
[[NSColor grayColor] set];
[path stroke];
[theMapWindow setNeedsDisplayInRect:rect];
}
答案 0 :(得分:2)
你做错了。你不应该自己打电话给drawRect
,它会被你召唤。将drawRect
的调用替换为setNeedsDisplayInRect
,并从setNeedsDisplayInRect
方法中删除drawRect
。
答案 1 :(得分:1)
你的drawRect ::是自定义方法,与drawRect:不同,当你将视图作为当前上下文时,它不会被调用,尝试在你的周围抛出[self lockFocus]
,[self unlockFocus]
绘图代码。
答案 2 :(得分:1)
NSView
的{{1}}方法;您应该将它用于您的绘图,如Drawing View Content中所述。
drawRect:
-
@interface mapWindow : NSView {
@private
NSView* theMapWindow;
NSPoint drawPoint;
}
// - (void)drawRect:(int)pointx: (int)pointy;
@property (assign) IBOutlet NSView* theMapWindow;
@property (assign) NSPoint drawPoint;
@end