3

I usually use this method to implement the UIButton movable;

[button addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents:UIControlEventTouchDragInside];

But it will trigger the touchUpInside event at same time, and I need touchUpInside event to do some other things.

So does anyone know how to avoid this?

Thanks a lot;

use these code below to solve it;

  [button addTarget:self action:@selector(touchDown:withEvent:) forControlEvents:UIControlEventTouchDown];

    [button addTarget:self action:@selector(touchDragInside:withEvent:) forControlEvents:UIControlEventTouchDragInside];
        [button addTarget:self action:@selector(touchUpInside:withEvent:) forControlEvents:UIControlEventTouchUpInside];

    - (void) touchUpInside :(UIControl*)c withEvent:ev{
    NSLog(@"touch Up inside");
    if (count) {
        NSLog(@"here I can do something about clicking event");
    }
}

    - (void) touchDown:(UIControl*)c withEvent:ev{
    NSLog(@"touch Down");
    count = YES;
}

    -(void) touchDragInside:(UIControl*)c withEvent:ev{
    NSLog(@"touch drag inside");
    c.center = [[[ev allTouches] anyObject] locationInView:self.view];
    count = NO;
}
Desdenova
  • 5,326
  • 8
  • 37
  • 45
zedzhao
  • 517
  • 1
  • 3
  • 17
  • Check out the possible [duplicate for this](http://stackoverflow.com/questions/15588604/how-to-implement-two-ibactions-in-uibutton-without-overlap) It will help you. – Nishant Tyagi Apr 20 '13 at 09:08
  • sovled it. I just make a flag in touchUpInside event. – zedzhao Apr 21 '13 at 15:06

1 Answers1

1

Actually the best way to accomplish this is to use UIPanGestureRecognizer and add it to the button. It handles the dragging very well and you also can tap on it without needing to do anything else.

GeneCode
  • 7,545
  • 8
  • 50
  • 85