을에 사각형을 드래그하면 :코코아
이 작업을 수행하는 코드는 다음과 같습니다
// Get the starting location of the mouse down event.
NSPoint location = [self convertPoint: [event locationInWindow] fromView: nil];
// Break out if this is not within the bounds of the rect.
if (!NSPointInRect(location, [self boundsOfAllControlPoints])) {
return;
}
while (YES) {
// Begin modal mouse tracking, looking for mouse dragged and mouse up events
NSEvent *trackingEvent = [[self window] nextEventMatchingMask:(NSLeftMouseDraggedMask | NSLeftMouseUpMask)];
// Get tracking location and convert it to point in the view.
NSPoint trackingLocation = [self convertPoint:[trackingEvent locationInWindow] fromView:nil];
// Calculate the delta's of x and y compared to the previous point.
long dX = location.x - trackingLocation.x;
long dY = location.y - trackingLocation.y;
// Update all points in the rect
for (int i = 0; i < 4; i++) {
NSPoint newPoint = NSMakePoint(points[i].x - dX, points[i].y - dY);
points[i] = newPoint;
}
NSLog(@"Tracking location x: %f y: %f", trackingLocation.x, trackingLocation.y);
// Set current location as previous location.
location = trackingLocation;
// Ask for a redraw.
[self setNeedsDisplay:YES];
// Stop mouse tracking if a mouse up is received.
if ([trackingEvent type] == NSLeftMouseUp) {
break;
}
}
기본적으로 나는 마우스 이벤트를 잡아서 그 위치가 드래그 가능한 rect 안에 있는지 확인합니다. 그렇다면 trackingEvent에서 마우스의 움직임을 추적하기 시작합니다. x와 y 좌표에 대한 델타 값을 계산하고, 드래그 가능한 rect에 대한 새로운 포인트를 만들고, 뷰 디스플레이의 새로 고침을 요청합니다.
효과가 있지만 드래그하는 동안 조금 "아마추어"처럼 보입니다. 마우스 포인터는 드래그되는 모양을 따라 잡을 것이고 결국 경계를 넘을 것입니다. 다른 드래그 작업에서는 끌기 작업의 시작부터 끝까지 드래그되는 객체의 위치에 마우스 포인터가 고정되어 표시됩니다.
이 효과의 원인은 무엇입니까?
편집 : 내가 롭의 대답은 다음과 나의 접근 방식을 변경하고 세 가지 방법의 접근 방식을 채택했습니다
: 효과가 약간 더 있지만
- (void) mouseDown: (NSEvent*) event {
// There was a mouse down event which might be in the thumbnail rect.
[self setDragStartPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
// Indicate we have a valid start of a drag.
if (NSPointInRect([self dragStartPoint], [self boundsOfAllControlPoints])) {
[self setValidDrag: YES];
}
}
- (void) mouseDragged: (NSEvent *) anEvent {
// Return if a valid drag was not detected during a mouse down event.
if (![self validDrag]) {
return;
}
NSLog(@"Tracking a drag.");
// Get tracking location and convert it to point in the view.
NSPoint trackingLocation = [self convertPoint: [anEvent locationInWindow] fromView: nil];
// Calculate the delta's of x and y compared to the previous point.
long dX = [self dragStartPoint].x - trackingLocation.x;
long dY = [self dragStartPoint].y - trackingLocation.y;
// Update all points in the rect
for (int i = 0; i < 4; i++) {
NSPoint newPoint = NSMakePoint(points[i].x - dX, points[i].y - dY);
points[i] = newPoint;
}
// Ask for a redraw.
[self setNeedsDisplay:YES];
NSLog(@"Tracking location x: %f y: %f", trackingLocation.x, trackingLocation.y);
// Set current location as previous location.
[self setDragStartPoint: trackingLocation];
NSLog(@"Completed mouseDragged method. Allow for repaint.");
}
- (void) mouseUp: (NSEvent *) anEvent {
// End the drag.
[self setValidDrag: NO];
[self setNeedsDisplay: YES];
}
을 여전히 RECT와 눈에 띄는 지연있다 결국 마우스 포인터의 움직임 방향 뒤에서 드래그합니다. 드래그하는 동안 마우스를 천천히 움직이면 특히 눈에.니다.
편집 2 :
알아보기 문제는 델타 계산에있었습니다. 저는 플로트를 사용해야하는 동안 오랫동안 사용했습니다. 이제 잘됩니다.
마우스를 끌기위한 코드를 공유해 주셔서 감사합니다. – Tommy