I suggest not using deltaY and deltaY ; try using the location of the event in supervision. You will need a link to the preview.
You would do the same bounds check in mouseUp:
UPDATE:. You should also take a look at the Programming Guide in a view in which you are prompted to create a drag-and-drop view: Create a Custom View .
Sample code that should be useful, although it has no strict relevance to your original question:
In DotView.m:
- (void)drawRect:(NSRect)dirtyRect { // Ignoring dirtyRect for simplicity [[NSColor colorWithDeviceRed:0.85 green:0.8 blue:0.8 alpha:1] set]; NSRectFill([self bounds]); // Dot is the custom shape class that can draw itself; see below // dots is an NSMutableArray containing the shapes for (Dot *dot in dots) { [dot draw]; } } - (void)mouseDown:(NSEvent *)event { NSPoint mousePoint = [self convertPoint:[event locationInWindow] fromView:nil]; currMovingDot = [self clickedDotForPoint:mousePoint]; // Move the dot to the point to indicate that the user has // successfully "grabbed" it if( currMovingDot ) currMovingDot.position = mousePoint; [self setNeedsDisplay:YES]; } // -mouseDragged: already defined earlier in post - (void)mouseUp:(NSEvent *)event { if( !currMovingDot ) return; NSPoint mousePoint = [self convertPoint:[event locationInWindow] fromView:nil]; spot.x = MAX(0, MIN(mousePoint.x, self.bounds.size.width)); spot.y = MAX(0, MIN(mousePoint.y, self.bounds.size.height)); currMovingDot.position = mousePoint; currMovingDot = nil; [self setNeedsDisplay:YES]; } - (Dot *)clickedDotForPoint:(NSPoint)point { // DOT_NUCLEUS_RADIUS is the size of the // dot internal "handle" for( Dot *dot in dots ){ if( (abs(dot.position.x - point.x) <= DOT_NUCLEUS_RADIUS) && (abs(dot.position.y - point.y) <= DOT_NUCLEUS_RADIUS)) { return dot; } } return nil; }
Dot.h
#define DOT_NUCLEUS_RADIUS (5) @interface Dot : NSObject { NSPoint position; } @property (assign) NSPoint position; - (void)draw; @end
Dot.m
As you can see, the Dot class is very lightweight and uses bezier paths for drawing. The supervisor can handle user interaction.
source share