ayon
ayon

Reputation: 2180

Detect long press on UIButton in iOS Simulator

I have a UIButton in my Custom UITableViewCell. I am working on some control events on that button in the UITableViewCell by the following code. This is taken from CellForRowAtIndexPath method.

    cell.gestureButton.tag = indexPath.row ;

    [cell.gestureButton addTarget:self action:@selector(cellTapped:) forControlEvents:UIControlEventTouchUpInside];

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(cellLongPressed:)];
    lpgr.minimumPressDuration = 2.0; //seconds
    lpgr.delegate = self ;
    [cell.gestureButton addGestureRecognizer:lpgr];

I am testing this on iOS 7 Simulator. My problem is , for the first event when UIControlEventTouchUpInside is executed , I can see the result and my cellTapped method is called properly.

But in the second case where I have assigned a UILongPressGestureRecognizer on my button , I can't see the result in simulator and cellLongPressed: method is never called. As far I understand, my code is ok. So, I would like to know , where's the problem ? Is there any problem with my code or Simulator doesn't support this feature ? Thanks in advance for your help.

Upvotes: 1

Views: 5345

Answers (2)

Aaron Brager
Aaron Brager

Reputation: 66252

I'm betting lpgr is conflicting with another gesture recognizer. Have you tried implementing UILongPressGestureRecognizer's delegate methods? You may need to set up a failure dependency. Specifically, you'll probably need to return YES in gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:.

Upvotes: 2

Yas Tabasam
Yas Tabasam

Reputation: 10615

Make sure your cellLongPressed is declared as following.

- (void)cellLongPressed:(UIGestureRecognizer *)gestureRecognizer {
   NSLog(@"cellLongPressed in action");
}

Or if its declared as following:

- (void)cellLongPressed {
   NSLog(@"cellLongPressed in action");
}

Please change your gesture initializer to following:

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(cellLongPressed)];

Note there is no ":" at the end of cellLongPressed selector name.

Upvotes: 1

Related Questions