Dragging an NSImageView

Discussion in 'Mac Programming' started by Blakeasd, Aug 4, 2011.

  1. Blakeasd macrumors 6502a

    Dec 29, 2009
    I have an NSImageView that I need to drag it around. I know to use the mouseDragged: method in the NSImageView subclass. I looked in the Apple Documentation and I didn't see a property or method for getting the NSImageView's position. How can I use the mouseDragged: method so the user can drag the NSImageView only horizontally and vertically. The movement will also need to be animated.
  2. Blakeasd thread starter macrumors 6502a

    Dec 29, 2009
    I figured out how to move it around, but I don't know how to only allow horizontal and vertical movement (No moving it in circles and diagonal movement).
    Please Help
  3. Sydde macrumors 68020


    Aug 17, 2009
    Look up NSDraggingSource Protocol in the AppKit reference section. There is a method in there that might serve your need. However, AFAIK, you cannot constrain the cursor itself, the OS simply does not allow that (directly, at least), you will only be able to constrain the motion of the drag image.
  4. Blakeasd thread starter macrumors 6502a

    Dec 29, 2009
    I just want to limit the movement to up/down and side-to-side.
    I am using this to allow the dragging movement:
    - (void)mouseDown:(NSEvent *)theEvent
    	    lastDragLocation = [theEvent locationInWindow]; 
    -(void)mouseDragged:(NSEvent *)theEvent{
        NSPoint newDragLocation = [theEvent locationInWindow];
        NSPoint origin = [self frame].origin;
        origin.x += (-lastDragLocation.x + newDragLocation.x);
        origin.y += (-lastDragLocation.y + newDragLocation.y);
        [self setFrameOrigin:origin];
        lastDragLocation = newDragLocation;
    How could I modify this to only allow left/right and up/down movement?
  5. Comrade Yeti macrumors newbie

    Nov 3, 2010
    Ignore the X component, if you want to restrict to vertical only, or Y component to restrict to horizontal dragging only

Share This Page