Re: Need HELP - Dragging a view
Re: Need HELP - Dragging a view
- Subject: Re: Need HELP - Dragging a view
- From: Keith Wilson <email@hidden>
- Date: Fri, 3 Mar 2006 00:53:54 +1100
Thanks for the code Scott - yet another way of doing it - but doesn't
your proposal leave a white trail behind the dragged thing?
I've a windoze background and I miss the ROP functions for drawing
inverse pixels and then reverting them but Mac has so many other
wonderful things going for it that you really wonder what those dozey
guys in Seattle do all day !!
Anyway, in a previous post David said
<< IMPORTANT ADDITION - I did not use the drag & drop technologies
because I need Live dragging.>>
From which I assumed that the next thing he wants to draw are rubber-
bands showing connections to other objects.
- did I guess right David? If so then ... If not then what are your
really after?
Keith from DownUnder
On 02/03/2006, at 11:58 PM, Scott Anguish wrote:
well, he's redrawing the view, not the screen necessarily.
the technique you're suggesting is way overkill though. and you're
short circuiting the event loop. this is either good, or bad
depending on your position on this (it's a touchy subject, both
techniques do have their place though.
Here is a snippet of some upcoming doc code, although I've
simplified it further here to eliminate some of the factoring I did
for clean code for the doc. hopefully there is enough here to get
the gist of things. if you need to see the whole thing, it'll be
available shortly.
this is not slow though. lastDragLocation, dragging, and
draggableThingRect are both instance variables for the view class
- (void)drawRect:(NSRect)rect
{
// erase the background by drawing white
[[NSColor whiteColor] set];
[NSBezierPath fillRect:rect];
// set the current color for the draggable item
[[self itemColor] set];
// draw the draggable item
[NSBezierPath fillRect:draggableThingRect];
}
- (void)offsetLocationByX:(float)x andY:(float)y
{
// tell the display to redraw the old rect
[self setNeedsDisplayInRect:draggableThingRect];
// since the offset can be generated by both mouse moves
// and moveUp:, moveDown:, etc.. actions, we'll invert
// the deltaY amount based on if the view is flipped or
// not.
int invertDeltaY = [self isFlipped] ? -1: 1;
location.x=location.x+x;
location.y=location.y+y*invertDeltaY;
// invalidate the new rect location so that it'll
// be redrawn
[self setNeedsDisplayInRect:draggableThingRect];
}
// -----------------------------------
// Handle Mouse Events
// -----------------------------------
-(void)mouseDown:(NSEvent *)event
{
NSPoint clickLocation;
BOOL itemHit=NO;
// convert the click location into the view coords
clickLocation = [self convertPoint:[event locationInWindow]
fromView:nil];
// Yes it did, note that we're starting to drag
if (NSPointInRect(clickLocation,draggableThingRect) {
// flag the instance variable that indicates
// a drag was actually started
dragging=YES;
// store the starting click location;
lastDragLocation=clickLocation;
}
}
-(void)mouseDragged:(NSEvent *)event
{
if (dragging) {
NSPoint newDragLocation=[self convertPoint:[event locationInWindow]
fromView:nil];
// offset the item by the change in mouse movement
// in the event
[self offsetLocationByX:(newDragLocation.x-lastDragLocation.x)
andY:(newDragLocation.y-lastDragLocation.y)];
// save the new drag location for the next drag event
lastDragLocation=newDragLocation;
// support automatic scrolling during a drag
// by calling NSView's autoscroll: method
[self autoscroll:event];
}
}
-(void)mouseUp:(NSEvent *)event
{
dragging=NO;
// finished dragging, restore the cursor
[NSCursor pop];
}
_______________________________________________
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