hoa*_*Cap 4 objective-c uigesturerecognizer
我在这里遇到了长按手势的麻烦.我环顾四周,发现了一些与我的问题有关的帖子,但直到现在还没有运气.
我有一个长按手势的视图,我想在手势触发时显示警报视图,但不知何故,当显示警报视图时触发器被调用两次,我检查手势识别器的状态但仍然没运气.这里的代码:
初始代码:
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.
}
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,如果我删除[警报显示],一切都按预期进行,手势只触发一次.有人对此有解释吗?提前致谢.
请使用以下代码获取解决方案.
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.
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1119 次 |
| 最近记录: |