Re: What am I doing wrong in my drag loop? Dragging jumps all over the place.
Re: What am I doing wrong in my drag loop? Dragging jumps all over the place.
- Subject: Re: What am I doing wrong in my drag loop? Dragging jumps all over the place.
- From: "John C. Randolph" <email@hidden>
- Date: Wed, 20 Jul 2005 11:27:22 -0700
Dave,
I wouldn't recommend grabbing the event loop like this. Have a look
at the "Cropped Image" sample.
-jcr
On Jul 20, 2005, at 10:02 AM, Dave Camp wrote:
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
_______________________________________________
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