我正在尝试绘制一个矩形,并让用户根据单击和拖动鼠标的位置来创建它。
我用来绘制NSRect的代码是:
CGFloat width = endPoint.x-startPoint.x; //Width of rectangle.
CGFloat height = endPoint.y-startPoint.y; //Height of rectangle.
CGFloat rectXPoint = startPoint.x; //x component of corner of rect
CGFloat rectYPoint = startPoint.x; //y component of corner of rect
if (width < 0) { //Handle negavive rect size here.
width = startPoint.x-endPoint.x;
rectXPoint = endPoint.x;
}
if (height < 0) { //and here.
height = startPoint.y-endPoint.y;
rectYPoint = endPoint.y;
}
NSRect theRect = NSMakeRect(rectXPoint, rectYPoint, width, height);
[[NSColor blackColor] set];
NSRectFill(theRect);所以我可以让代码工作,但不完全是。有时,它会将矩形的y从光标偏移一些看似任意的量(即使我知道这不是任意的)。很难截取屏幕截图,因为这需要我将鼠标从需要演示的点上移动。为了防止你需要查看我如何获取鼠标位置的代码,这里是:
- (void)mouseDown:(NSEvent *)event {
NSPoint location = [self convertPointFromBase:[event locationInWindow]];
startPoint = location;
}
- (void)mouseDragged:(NSEvent *)event {
NSPoint location = [self convertPointFromBase:[event locationInWindow]];
endPoint = location;
[self setNeedsDisplay:YES];
}isFlipped返回YES。
如果您需要更多的澄清,请询问您需要澄清的内容。
发布于 2011-06-04 11:12:36
可能是下面这行:
CGFloat rectYPoint = startPoint.x; //y component of corner of recthttps://stackoverflow.com/questions/6234438
复制相似问题