使用 UITapGestureRecognizer
iPhone 开发新手。我有一个视图,其中包含 UIScrollView,其中包含 UIImageView。我在图像视图上添加了一个(双击)点击手势识别器,这使得警报框打开。由于某种原因,我确信我只是弱智,它打开了 3 次。
这是我的代码:
- (void)viewDidLoad {
scrollView.delegate = self;
UIImage* image = imageView.image;
imageView.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
scrollView.contentSize = image.size;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:tapGesture];
[tapGesture release];
NSLog(@"LOADED");
[super viewDidLoad];
}
-(IBAction) handleTapGesture:(UIGestureRecognizer *) sender {
CGPoint tapPoint = [sender locationInView:imageView];
int tapX = (int) tapPoint.x;
int tapY = (int) tapPoint.y;
NSLog(@"TAPPED X:%d Y:%d", tapX, tapY);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"How are you?" delegate:nil cancelButtonTitle:@"I'm awesome." otherButtonTitles:nil];
[alert show];
[alert release];
}
几天前我刚刚开始 iPhone 开发。这个问题有点让我想起我在 javascript 中处理过的事件冒泡问题。有什么想法吗?
New to iPhone dev. I have a view which contains a UIScrollView which contains a UIImageView. I added a (double) tap gesture recognizer on the image view which makes an alert box open. For some reason, and I'm sure I'm just retarded, it opens 3 times.
Here's my code:
- (void)viewDidLoad {
scrollView.delegate = self;
UIImage* image = imageView.image;
imageView.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
scrollView.contentSize = image.size;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:tapGesture];
[tapGesture release];
NSLog(@"LOADED");
[super viewDidLoad];
}
-(IBAction) handleTapGesture:(UIGestureRecognizer *) sender {
CGPoint tapPoint = [sender locationInView:imageView];
int tapX = (int) tapPoint.x;
int tapY = (int) tapPoint.y;
NSLog(@"TAPPED X:%d Y:%d", tapX, tapY);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"How are you?" delegate:nil cancelButtonTitle:@"I'm awesome." otherButtonTitles:nil];
[alert show];
[alert release];
}
I just started iPhone dev a few days ago. This problem kind of reminds me of event bubbling issues I've dealt with in javascript. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定确切的原因是什么,但 UIAlertView 不知何故导致手势再次触发。解决方法是使用 PerformSelector 在手势处理程序外部执行显示:
编辑:
手势识别器会经历手势中的不同状态(开始、更改等),并且每次状态更改时都会调用处理程序方法。因此,更好且可能正确的解决方案是检查处理程序顶部的手势识别器的状态属性:
Not sure what the exact reason is but the UIAlertView is somehow causing the gesture to fire again. A workaround is to execute the showing outside the gesture handler using performSelector:
Edit:
The gesture recognizer goes through different states in the gesture (Began, Changed, etc) and it calls the handler method each time the state changes. So a better and probably correct solution is to check the state property of the gesture recognizer at the top of the handler: