1

这段代码一直有效,直到我将我的项目从 ios4 转换为 ios6 (+ARC) 并将我的 xib 文件交换为情节提要。现在我所做的任何点击都算作长按。

手势设置

- (void)viewDidLoad
{

[super viewDidLoad];

for(UIButton *button in buttons)
{
    UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDetected:)];
    longPressRecognizer.minimumPressDuration = 1;
    longPressRecognizer.numberOfTouchesRequired = 1;
    [button addGestureRecognizer:longPressRecognizer];
}

}

长按法

- (IBAction)longPressDetected:(UIGestureRecognizer *)sender 
{
    if (sender.state != UIGestureRecognizerStateBegan) 
    {
        NSLog(@"duplicate press cancelled");
        return;
    }
    NSLog(@"LongPress Received");
}

故事板 在此处输入图像描述

4

2 回答 2

1

将您的代码替换为此然后检查:

UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]  initWithTarget:self action:@selector(longPressDetected:)];
longPressRecognizer.minimumPressDuration = 2.0;
longPressRecognizer.delegate = self;
[button addGestureRecognizer:longPressRecognizer];
于 2013-02-28T06:21:41.590 回答
1

根据您添加的屏幕截图,您已将按钮链接到longPressDetected:情节提要中。您需要在情节提要中将其删除,它会正常工作。

基本上它正在执行也指向相同方法的按钮操作。

于 2013-02-28T19:51:20.393 回答