NSRect に関する質問... Hillegass の本では、楕円 (NSBezierPath *) を描画する NSRect を作成しています。ビューのどこでマウスを押してドラッグしたかによって、NSRect の size.width および/または size.height が負になる場合があります (つまり、右上から開始して左下にドラッグすると、両方とも負になります)。実際に描画するとき、システムは負の幅や高さを使用して、ドラッグした場所の NSPoint を特定するだけですか? したがって、NSRect を更新しますか? NSRect のサイズが必要な場合は、絶対値を使用する必要がありますか?
この章では、著者は MIN() および MAX() マクロを使用して NSRect を作成しました。ただし、チャレンジ ソリューションでは、マウス イベントに応答して次の 3 つのメソッドを提供します。
- (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 を適切に計算する際に、舞台裏で何が起こっているのでしょうか?