处理与论点iphone / ipad的轻拍手势 [英] handle tap gesture with an argument iphone / ipad

查看:103
本文介绍了处理与论点iphone / ipad的轻拍手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的点击手势触发时,我需要发送一个额外的参数,但我必须做一些非常愚蠢的事情,我在这里做错了什么:

when my tap gesture fires I need to send an additional argument along with it but I must be doing something really silly, what am I doing wrong here:

这里是我的手势被创建和添加:

Here is my gesture being created and added:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:itemSKU:)];
tapGesture.numberOfTapsRequired=1;
[imageView setUserInteractionEnabled:YES];
[imageView addGestureRecognizer:tapGesture];
[tapGesture release];

[self.view addSubview:imageView];

这是我处理的地方:

-(void) handleTapGesture:(UITapGestureRecognizer *)sender withSKU: (NSString *) aSKU {
        NSLog(@"SKU%@\n", aSKU);
}

由于UITapGestureRecognizer init行而无法运行。

and this won't run because of the UITapGestureRecognizer init line.

我需要知道可以识别哪些图像被点击的内容。

I need to know something identifiable about what image was clicked.

推荐答案

A手势识别器只会将一个参数传递给动作选择器:本身。我假设你试图区分主视图的各种图像子视图上的点击?在这种情况下,最好的办法是调用 -locationInView:,传递superview,然后调用 -hitTest:withEvent:在该视图上生成 CGPoint 。换句话说,就像这样:

A gesture recognizer will only pass one argument into an action selector: itself. I assume you're trying to distinguish between taps on various image subviews of a main view? In that case, your best bet is to call -locationInView:, passing the superview, and then calling -hitTest:withEvent: on that view with the resulting CGPoint. In other words, something like this:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
...
- (void)imageTapped:(UITapGestureRecognizer *)sender
{
    UIView *theSuperview = self.view; // whatever view contains your image views
    CGPoint touchPointInSuperview = [sender locationInView:theSuperview];
    UIView *touchedView = [theSuperview hitTest:touchPointInSuperview withEvent:nil];
    if([touchedView isKindOfClass:[UIImageView class]])
    {
        // hooray, it's one of your image views! do something with it.
    }
}

这篇关于处理与论点iphone / ipad的轻拍手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆