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];
}