我在这里遇到了长按手势的麻烦。我环顾四周,发现了一些与我的问题有关的帖子,但直到现在还没有运气。
我对一个视图有一个长按手势,我想在触发手势时显示警报视图,但是当显示警报视图时触发器被调用了两次,我检查了状态手势识别器,但仍然没有运气。代码如下:
初始代码:
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[longTap setMinimumPressDuration:1];
[self addGestureRecognizer:longTap];
- (IBAction)handleTapGesture:(UIPanGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateChanged) {
NSLog(@"Change");
} else if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"Ended");
}
else {
NSLog(@"Begin");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Long pressed" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show]; //If I remove this line, the trigger is call only once.
}
}
奇怪的是,如果我删除[提示节目],一切都按预期进行,手势只触发一次。 有人对此有解释吗?提前致谢。
答案 0 :(得分:5)
请使用以下代码获取解决方案。
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[longTap setMinimumPressDuration:1];
longTap.delegate = (id)self;
[self.view addGestureRecognizer:longTap];
- (void)handleTapGesture:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateChanged)
{
NSLog(@"Change");
}
else if (sender.state == UIGestureRecognizerStateEnded)
{
NSLog(@"Ended");
}
else if (sender.state == UIGestureRecognizerStateBegan)
{
NSLog(@"Begin");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Long pressed" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show]; //If I remove this line, the trigger is call only once.
}
}