0

In the app, I have couple of dynamic buttons which is created and added to UITableView, each buttons having a touch event (UIControlEventTouchUpInside) and a long press gesture (UILongPressGestureRecognizer), I want to perform any one action at a time. So if when user touches only button action will be call. And if user has long pressed then long press event will be called.

Currently, it's always calling action even if I have long press on the button.

What should I do handle this events? Any good suggestions?

Hemang
  • 5
  • 3
  • Duplicate: http://stackoverflow.com/questions/30859203/uibutton-with-single-press-and-long-press-events-swift – Frankie Nov 14 '16 at 12:33
  • 2
    Possible duplicate of [UIbutton with longpress and Touchup inside](http://stackoverflow.com/questions/6660282/uibutton-with-longpress-and-touchup-inside) – Narendra Pandey Nov 14 '16 at 12:41

4 Answers4

2

You can add below code in button action event. I have done this code for multiple checkbox in tableview. With the help of this code, you can get IndexPath of tableview record. I hope it's work for you.

- (IBAction)btnPressed:(UIButton *)sender {

CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.tblView];
NSIndexPath *indexpath = [self.tblView indexPathForRowAtPoint:touchPoint];
 NSLog(@"%ld",(long)indexpath.row);
}
Anand Nanavaty
  • 554
  • 4
  • 13
0

In the cellForRowAtIndex of Tableview use this code:

[cell.btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];

And outside of the cellForRowAtIndex implement this method.

-(void)btnPressed:(UIButton*)btn
{
   //Do whatever
} 
Poles
  • 3,585
  • 9
  • 43
  • 91
0

In order not to trigger both you should flag the button with a global variable or a tag, so in the target of UIControlEventTouchUpInside you can filter the action.

So let's assume your UILongPressGestureRecognizer calls longPress when fires, and it's initialize in your custom cell. & UIControlEventTouchUpInside calls the target btnPressed

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.button addGestureRecognizer:longPress];
[self.button addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];

Selector call, inside your custom cell:

-(void)longPress:(UIButton*)btn
{
   // Flag the button,
   self.button.tag = 1;

   // Do LongPress stuff.  

}

Target of the button for UIControlEventTouchUpInside

- (void)btnPressed:(id)sender { 

    UIButton *senderButton = sender;

    if(senderButton.tag == 1) {
        // Long press has been executed, set back the flag to 0
        senderButton.tag = 0;
    } else {
        // Long press not executed 
        // Do the TouchUpInside stuff.
    }
}
Pau Senabre
  • 4,155
  • 2
  • 27
  • 36
-1

And for long press use this code

`UILongPressGestureRecognizer` *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [cell.button addGestureRecognizer:longPress];

and long press method is

-(void)longPress:(UIButton*)btn
{
   //Do whatever
} 
Narendra Pandey
  • 377
  • 9
  • 28