0
我想绘制一个矩形并让用户根据他点击的位置以及他拖动鼠标的位置来创建它。我不能根据用户点击的位置绘制NSRect
我使用绘制的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。
如果您需要更多解释,请询问您需要澄清的事项。
喔。 .Y。我真笨。大声笑。 – Dair 2011-06-04 03:14:00