此代码一直工作,直到我将项目从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];
}
}
LongPress方法
- (IBAction)longPressDetected:(UIGestureRecognizer *)sender
{
if (sender.state != UIGestureRecognizerStateBegan)
{
NSLog(@"duplicate press cancelled");
return;
}
NSLog(@"LongPress Received");
}
故事板
答案 0 :(得分:1)
将代码替换为此,然后检查:
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDetected:)];
longPressRecognizer.minimumPressDuration = 2.0;
longPressRecognizer.delegate = self;
[button addGestureRecognizer:longPressRecognizer];
答案 1 :(得分:1)
根据您添加的屏幕截图,您已将故障板中的按钮链接到longPressDetected:
。你需要在故事板中删除它,它会正常工作。
基本上它正在执行按钮动作,该动作也指向相同的方法。