Reputation: 3061
Is there any way of making an NSButton draggable? I mean like in the final built application, the user can drag the NSButton.
I'm trying to do something almost "Dashboard" style... like draggable things. At the moment, I'm just using NSButtons that stay in place, but it'd be nice if they could be dragged around.
Upvotes: 2
Views: 1578
Reputation: 560
NSButton normally eats up all mouse events. Therefore you must subclass NSButton and override not only the dragging handling but also the click handling:
- (void)mouseDown:(NSEvent *)theEvent
{
[self highlight:YES];
}
- (void)mouseDragged:(NSEvent *)event
{
// start a dragging session
}
- (void)mouseUp:(NSEvent *)theEvent
{
[self performClick:self];
}
Upvotes: 6
Reputation: 5990
To add depth:
You would need to subclass the NSButton and overide the:
-(void)mouseDragged:(NSEvent *)theEvent{
//With:
NSRect gframe = self.frame;
gframe.origin.x = theEvent.locationInWindow.x;
[self setFrame:gframe];
}
Upvotes: 1
Reputation: 299265
I'd start with the standard Drag-and-drop infrastructure. Even though you may not be using all the features, it's generally a good starting point and handles a lot of tricky cases for you.
Upvotes: 3
Reputation: 2781
Take a look at "continueTrackingWithTouch:withEvent:" in the UIControl class, UITouch and the Event Handling guide. The basic idea is to make a handler for the TouchDragInside event, using the location data from the event to update the center of the button.
This would be such handy advice if you were working on iOS, which you aren't.
From a quick glance at a few thing in NSControl and NSView, I'm sure it can be done, I just don't have a quick answer, and I'm short on time. :(
Basically, the idea is to catch the mouseDown events, and start paying attention to the mouse location, updating the location of the button by the same relative amount that the mouse moves each time you check.
More later if needed.
Upvotes: 0