Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting when UIButton event is done

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");
}
like image 311
Snowman Avatar asked Nov 22 '25 10:11

Snowman


1 Answers

I think that replacing

UIControlEventTouchDragExit

with

UIControlEventTouchDragExit|UIControlEventTouchUpInside

would be a good start to solve your problem.

like image 194
mvds Avatar answered Nov 24 '25 01:11

mvds