NSRect 관련 질문 ... Hillegass 서적에서 우리는 타원형 (NSBezierPath *)을 그리는 NSRect를 만듭니다. 우리가 볼 수있는 위치에 따라 마우스를 누르고 드래그하면 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를 올바르게 계산할 때 배후에서 무슨 일이 벌어지고 있습니까?