简体   繁体   中英

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.

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: .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM