简体   繁体   English

在iOS模拟器中检测长按UIButton

[英]Detect long press on UIButton in iOS Simulator

I have a UIButton in my Custom UITableViewCell . 我的自定义UITableViewCell有一个UIButton I am working on some control events on that button in the UITableViewCell by the following code. 我正在通过以下代码在UITableViewCell上的该按钮上处理一些控件事件。 This is taken from CellForRowAtIndexPath method. 这取自CellForRowAtIndexPath方法。

    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. 我在iOS 7模拟器上测试它。 My problem is , for the first event when UIControlEventTouchUpInside is executed , I can see the result and my cellTapped method is called properly. 我的问题是,对于执行UIControlEventTouchUpInside的第一个事件,我可以看到结果并正确调用了我的cellTapped方法。

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. 但在我的按钮上分配了UILongPressGestureRecognizer的第二种情况下,我无法在模拟器和cellLongPressed:看到结果cellLongPressed:从不调用方法。 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 ? 我的代码有什么问题,或者Simulator不支持此功能吗? Thanks in advance for your help. 在此先感谢您的帮助。

I'm betting lpgr is conflicting with another gesture recognizer. 我打赌lpgr与另一个手势识别器冲突。 Have you tried implementing UILongPressGestureRecognizer's delegate methods? 您是否尝试过实施UILongPressGestureRecognizer的委托方法? You may need to set up a failure dependency. 您可能需要设置故障依赖性。 Specifically, you'll probably need to return YES in gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: . 具体来说,你可能需要在gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:返回YES gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: .

Make sure your cellLongPressed is declared as following. 确保您的cellLongPressed声明如下。

- (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. 请注意, cellLongPressed选择器名称末尾没有“ ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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