• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Need HELP - Dragging a view
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Need HELP - Dragging a view


  • Subject: Re: Need HELP - Dragging a view
  • From: Scott Anguish <email@hidden>
  • Date: Thu, 2 Mar 2006 07:58:30 -0500

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


  • Follow-Ups:
    • Re: Need HELP - Dragging a view
      • From: Keith Wilson <email@hidden>
References: 
 >Re: Need HELP - Dragging a view (From: "" <email@hidden>)
 >Re: Need HELP - Dragging a view (From: Keith Wilson <email@hidden>)

  • Prev by Date: Re: Transparent window
  • Next by Date: Resizable inline images in NSTextView
  • Previous by thread: Re: Need HELP - Dragging a view
  • Next by thread: Re: Need HELP - Dragging a view
  • Index(es):
    • Date
    • Thread