我正在尝试绘制一个矩形,让用户根据他点击的位置和拖动鼠标的位置创建它。
我用来绘制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。
如果您需要进一步澄清,请询问您需要澄清的内容。
答案 0 :(得分:1)
可能这一行:
CGFloat rectYPoint = startPoint.x; //y component of corner of rect