繁体   English   中英

在iOS模拟器中检测长按UIButton

[英]Detect long press on UIButton in iOS Simulator

我的自定义UITableViewCell有一个UIButton 我正在通过以下代码在UITableViewCell上的该按钮上处理一些控件事件。 这取自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];

我在iOS 7模拟器上测试它。 我的问题是,对于执行UIControlEventTouchUpInside的第一个事件,我可以看到结果并正确调用了我的cellTapped方法。

但在我的按钮上分配了UILongPressGestureRecognizer的第二种情况下,我无法在模拟器和cellLongPressed:看到结果cellLongPressed:从不调用方法。 据我所知,我的代码还可以。 所以,我想知道,问题出在哪里? 我的代码有什么问题,或者Simulator不支持此功能吗? 在此先感谢您的帮助。

我打赌lpgr与另一个手势识别器冲突。 您是否尝试过实施UILongPressGestureRecognizer的委托方法? 您可能需要设置故障依赖性。 具体来说,你可能需要在gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:返回YES gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: .

确保您的cellLongPressed声明如下。

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

或者如果声明如下:

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

请将您的手势初始化程序更改为:

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

请注意, cellLongPressed选择器名称末尾没有“ ”。

暂无
暂无

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

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