1

Possible Duplicate:
UIButton long press with finger stationary

I have created 100 buttons from code. Now I want to react on long button press. For each buttons I call enxt code:

UILongPressGestureRecognizer *longPressGesture = [[[UILongPressGestureRecognizer alloc]
                                                  initWithTarget:self
                                                  action:@selector(longPress:)]
                                                  autorelease];
[longPressGesture setMinimumPressDuration:1];
[button addGestureRecognizer:longPressGesture];
[self.view addSubview:button];

But longPress method was not called.

Does anybody know why?

Community
  • 1
  • 1
Radislav
  • 2,892
  • 7
  • 38
  • 61
  • the `:` in the selector suppose `longPress` takes a parameter. Is that the case ? if not, try building the correct selector, without `:`. Of course `self` must respond to it too. –  Feb 22 '12 at 09:37
  • This is signature of my method: -(void)longPress:(UILongPressGestureRecognizer*)gesture. It works if I add recognaizer to my view, not to button. But I need to work with buttons created by myself in code. – Radislav Feb 22 '12 at 09:41

2 Answers2

2

It's quite likely that UIButton already uses a gesture recogniser for its touch handling. It's also quite likely that the tap gesture succeeds before your long tap recogniser is allowed to see the touches, and therefore your long touch never gets called.

My suggestion would be not use buttons and use views instead. You can add your long touch gesture recogniser to the view and change it's appearance behaviour to look and feel like a button and be able to keep your long touch.

Jasarien
  • 58,279
  • 31
  • 157
  • 188
  • I have tried use view it works for all elements on view, but I need only buttons. – Radislav Feb 22 '12 at 09:48
  • You're misunderstanding me. I'm saying use views instead of buttons, entirely. Forget about using buttons. Have your main view with 100 subviews that behave like buttons, only with your long press gesture. – Jasarien Feb 22 '12 at 15:21
1

You could look at this reponse to a related question : https://stackoverflow.com/a/6179591/536308

Community
  • 1
  • 1
ıɾuǝʞ
  • 2,829
  • 26
  • 38