有關NSRect的問題...在Hillegass書中,我們創建了一個NSRect,在其中繪製一個橢圓(NSBezierPath *)。根據我們視圖中的位置,我們將鼠標向下拖動,然後拖動,NSRect的size.width和/或size.height可能是負值(即,如果我們從右上角開始,拖動左下角 - 均爲負值)。實際繪製時,系統是否使用我們的負寬度和/或高度來僅僅定位我們拖動的位置的NSPoint?因此更新NSRect?如果我們需要NSRect的大小,我們是否應該使用絕對值?更新NSRect大小爲負值
在本章中,作者使用MIN()和MAX()宏創建NSRect。然而,在測試溶液它們提供響應這三種方法鼠標事件:不管潛在負值
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint pointInView = [self convertPoint:[theEvent locationInWindow] fromView:nil];
// Why do we offset by 0.5? Because lines drawn exactly on the .0 will end up spread over two pixels.
workingOval = NSMakeRect(pointInView.x + 0.5, pointInView.y + 0.5, 0, 0);
[self setNeedsDisplay:YES];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
NSPoint pointInView = [self convertPoint:[theEvent locationInWindow] fromView:nil];
workingOval.size.width = pointInView.x - (workingOval.origin.x - 0.5);
workingOval.size.height = pointInView.y - (workingOval.origin.y - 0.5);
[self setNeedsDisplay:YES];
}
- (void)mouseUp:(NSEvent *)theEvent
{
[[self document] addOvalWithRect:workingOval];
workingOval = NSZeroRect; // zero rect indicates we are not presently drawing
[self setNeedsDisplay:YES];
}
此代碼生成一個成功的矩形。我明白,負面價值觀僅僅反映了原點(我們「鼠標擊倒」)的轉變。在正確計算我們拖動的NSPoint的幕後發生了什麼?