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");
}
I think that replacing
UIControlEventTouchDragExit
with
UIControlEventTouchDragExit|UIControlEventTouchUpInside
would be a good start to solve your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With