Reputation: 32061
I'm trying to implement a drag & drop feature on a UIButton, and it works fine, but I can't figure out a way to determine when the user has let go of the button and finished dragging. The below code works fine for dragging, but I need to be notified when the user has finished dragging and let go of the button.
- (void)viewDidLoad
{
[gesturesBrowserButton addTarget:self action:@selector(wasDragged:withEvent:)
forControlEvents:UIControlEventTouchDragInside];
[gesturesBrowserButton addTarget:self action:@selector(finishedDragging:withEvent:)
forControlEvents:UIControlEventTouchDragExit];
}
- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
// get the touch
UITouch *touch = [[event touchesForView:button] anyObject];
// get delta
CGPoint previousLocation = [touch previousLocationInView:button];
CGPoint location = [touch locationInView:button];
CGFloat delta_x = location.x - previousLocation.x;
CGFloat delta_y = location.y - previousLocation.y;
// move button
button.center = CGPointMake(button.center.x + delta_x,
button.center.y + delta_y);
NSLog(@"was dragged");
}
- (void)finishedDragging:(UIButton *)button withEvent:(UIEvent *)event
{
//doesn't get called
NSLog(@"finished dragging");
}
Upvotes: 3
Views: 1652
Reputation: 47034
I think that replacing
UIControlEventTouchDragExit
with
UIControlEventTouchDragExit|UIControlEventTouchUpInside
would be a good start to solve your problem.
Upvotes: 11
Reputation: 299265
The event I believe you want here is UIControlEventTouchUpInside
. That's the "touch has gone away while inside this control." It's possible you won't get that one at this point, though. You certainly won't get UIControlEventTouchDragExit
, since that means the drag has left the control.
If UIControlEventTouchUpInside
doesn't work for you, I like to capture UIControlEventAllEvents
and just log them as they come in. Then you can see exactly what is fired when.
Upvotes: 2