Cocoa 我能';t根据用户单击的位置绘制NSRect

Cocoa 我能';t根据用户单击的位置绘制NSRect,cocoa,quartz-graphics,Cocoa,Quartz Graphics,我试图画一个矩形,让用户根据点击和拖动鼠标的位置来创建它 我用于绘制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 r

我试图画一个矩形,让用户根据点击和拖动鼠标的位置来创建它

我用于绘制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);
我翻了一下,回答是的

如果您需要更多的澄清,请询问您需要澄清的内容

可能是这一行:

CGFloat rectYPoint = startPoint.x; //y component of corner of rect
CGFloat rectYPoint = startPoint.x; //y component of corner of rect