使用 UITapGestureRecognizer

发布于 2024-09-28 20:00:23 字数 1180 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

極樂鬼 2024-10-05 20:00:23

不确定确切的原因是什么,但 UIAlertView 不知何故导致手势再次触发。解决方法是使用 PerformSelector 在手势处理程序外部执行显示:

-(void) 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);
    [self performSelector:@selector(showMessage) withObject:nil afterDelay:0.0];
}

- (void)showMessage
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"How are you?" delegate:nil cancelButtonTitle:@"I'm awesome." otherButtonTitles:nil];
    [alert show];
    [alert release];
}

编辑:
手势识别器会经历手势中的不同状态(开始、更改等),并且每次状态更改时都会调用处理程序方法。因此,更好且可能正确的解决方案是检查处理程序顶部的手势识别器的状态属性:

-(void) handleTapGesture:(UIGestureRecognizer *) sender {
    if (sender.state != UIGestureRecognizerStateEnded)  // <---
        return;                                         // <---

    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];
}

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:

-(void) 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);
    [self performSelector:@selector(showMessage) withObject:nil afterDelay:0.0];
}

- (void)showMessage
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"How are you?" delegate:nil cancelButtonTitle:@"I'm awesome." otherButtonTitles:nil];
    [alert show];
    [alert release];
}

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:

-(void) handleTapGesture:(UIGestureRecognizer *) sender {
    if (sender.state != UIGestureRecognizerStateEnded)  // <---
        return;                                         // <---

    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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文