Re: What am I doing wrong in my drag loop? [solved]
Re: What am I doing wrong in my drag loop? [solved]
- Subject: Re: What am I doing wrong in my drag loop? [solved]
- From: Dave Camp <email@hidden>
- Date: Wed, 20 Jul 2005 11:35:10 -0700
For whatever reason there seems to be some sort of synchronization
issue with superview and subview coordinates when in a loop like
this. I got the code working by changing all the convertPoint calls
to convert to superview coordinates and use those exclusively.
Dave
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:
40criticalpath.com
This email sent to email@hidden
---
The path of least resistance, it's not just for electricity any more.
_______________________________________________
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