What am I doing wrong in my drag loop? Dragging jumps all over the place.
What am I doing wrong in my drag loop? Dragging jumps all over the place.
- Subject: What am I doing wrong in my drag loop? Dragging jumps all over the place.
- From: Dave Camp <email@hidden>
- Date: Wed, 20 Jul 2005 10:02:41 -0700
I've got a subclass of NSView that I want to be able to drag around
in my superview. I've implemented in the following code, but it
doesn't work right...
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint startPoint;
BOOL isMoving = NO;
NSRect frame = [self frame];
// Get the offset of the cursor to the frame origin
startPoint = [self convertPoint:[theEvent locationInWindow]
fromView:nil];
float offsetX = startPoint.x = frame.origin.x;
float offsetY = startPoint.y = frame.origin.y;
while (YES)
{
theEvent = [[self window] nextEventMatchingMask:
(NSLeftMouseDraggedMask | NSLeftMouseUpMask)];
NSPoint curPoint = [self convertPoint:[theEvent
locationInWindow] fromView:nil];
NSLog(@"%g, %g", curPoint.x, curPoint.y);
if (!isMoving && ((fabs(curPoint.x - startPoint.x) >= 2.0)
|| (fabs(curPoint.y - startPoint.y) >= 2.0)))
isMoving = YES;
if (isMoving)
{
[[self superview] setNeedsDisplayInRect:frame];
curPoint.x += offsetX;
curPoint.y += offsetY;
frame.origin = curPoint;
[self setFrame:frame];
[self setNeedsDisplay:YES];
}
if ([theEvent type] == NSLeftMouseUp)
break;
}
}
What seems to happen is that on the first pass through the loop,
[self convertPoint:fromView:] returns the event location as expected.
On the next pass, it returns coordinates that are way off. This ping-
pongs back and forth as you drag. It seems to be caused by the [self
setFrame:frame] call. If I comment that out, the coordinates are as
expected.
This suggests that I'm not not doing something right, or missing a
needed call, when I change my view's frame while dragging. Is there
something I need to do to get the superviews to always be in sync
with my location?
Thanks,
Dave
---
It is dark; you are likely to be eaten by a grue. -Zork
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden