这段代码一直有效,直到我将我的项目从 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");
}
故事板