点击手势识别器 – 哪个对象被点击?

我是手势识别器的新手,所以也许这个问题听起来很愚蠢:我将轻拍手势识别器分配给一堆UIViews。 在这个方法中,有可能找出哪个是被点击的,或者我需要使用屏幕上的点来找出它?

for (NSUInteger i=0; i<42; i++) { float xMultiplier=(i)%6; float yMultiplier= (i)/6; float xPos=xMultiplier*imageWidth; float yPos=1+UA_TOP_WHITE+UA_TOP_BAR_HEIGHT+yMultiplier*imageHeight; UIView *greyRect=[[UIView alloc]initWithFrame:CGRectMake(xPos, yPos, imageWidth, imageHeight)]; [greyRect setBackgroundColor:UA_NAV_CTRL_COLOR]; greyRect.layer.borderColor=[UA_NAV_BAR_COLOR CGColor]; greyRect.layer.borderWidth=1.0f; greyRect.userInteractionEnabled=YES; [greyGridArray addObject:greyRect]; [self.view addSubview:greyRect]; NSLog(@"greyGrid: %i: %@", i, greyRect); //make them touchable UITapGestureRecognizer *letterTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(highlightLetter)]; letterTapRecognizer.numberOfTapsRequired = 1; [greyRect addGestureRecognizer:letterTapRecognizer]; } 

使用参数as定义您的目标select器( highlightLetter: :)

 UITapGestureRecognizer *letterTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(highlightLetter:)]; 

然后你可以看到

 - (void)highlightLetter:(UITapGestureRecognizer*)sender { UIView *view = sender.view; NSLog(@"%d", view.tag);//By tag, you can find out where you had tapped. } 

它已经有一年的时间提出这个问题,但仍然为某人。

在特定视图中声明UITapGestureRecognizer ,将标签分配为

 UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandlerMethod:)]; [yourGestureEnableView addGestureRecognizer:tapRecognizer]; yourGestureEnableView.tag=2; 

并在你的处理程序是这样的

 -(void)gestureHandlerMethod:(UITapGestureRecognizer*)sender { { if(sender.view.tag==2) { //do something here } } 

这是Swift 3的更新,也是Mani的答案的补充。 我会build议使用sender.view结合标记sender.view (或其他元素,这取决于你想跟踪什么)一些更“先进”的方法。

  1. 将UITapGestureRecognizer添加到例如一个UIButton(你也可以把它添加到UIViews等等)或者一个数组中的一大堆项目用for-loop和第二个数组作为轻敲手势。
  let yourTapEvent = UITapGestureRecognizer(target: self, action: #selector(yourController.yourFunction)) yourObject.addGestureRecognizer(yourTapEvent) // adding the gesture to your object 
  1. 在同一个testController中定义函数(这是你的视图控制器的名字)。 我们将在这里使用标签 – 标签是Int ID,您可以添加到您的UIView yourButton.tag = 1 。 如果你有一个像数组一样的元素的dynamic列表,你可以做一个for循环,它遍历你的数组并添加一个标签,它会递增

     func yourFunction(_ sender: AnyObject) { let yourTag = sender.view!.tag // this is the tag of your gesture's object // do whatever you want from here :) eg if you have an array of buttons instead of just 1: for button in buttonsArray { if(button.tag == yourTag) { // do something with your button } } } 

所有这一切的原因是因为在与#selector结合使用时,不能为yourFunction传递更多的参数。

如果你有一个更复杂的用户界面结构,你想获得项目的父母的标签附加到你的水龙头手势,你可以使用let yourAdvancedTag = sender.view!.superview?.tag例如获取button的UIView的标签内那UIView; 可用于缩略图+button列表等。

在Swift中使用这个代码

 func tappGeastureAction(sender: AnyObject) { if let tap = sender as? UITapGestureRecognizer { let point = tap.locationInView(locatedView) if filterView.pointInside(point, withEvent: nil) == true { // write your stuff here } } } 

您可以使用

  - (void)highlightLetter:(UITapGestureRecognizer*)sender { UIView *view = sender.view; NSLog(@"%d", view.tag); } 

视图将是识别轻击手势的对象

您应该修改手势识别器的创build以接受参数(添加冒号':')

 UITapGestureRecognizer *letterTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(highlightLetter:)]; 

在您的方法highlightLetter中:您可以访问附加到识别器的视图:

 -(IBAction) highlightLetter:(UITapGestureRecognizer*)recognizer { UIView *view = [recognizer view]; } 

您也可以使用UIGestureRecognizer的“shouldReceiveTouch”方法

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch: (UITouch *)touch { UIView *view = touch.view; NSLog(@"%d", view.tag); } 

不要忘记设置你的手势识别器的代表。

如果您在不同的UIView上添加不同的UIGestureRecognizer ,并且想要在操作方法中加以区分,那么您可以检查发件人参数中的属性视图,它将为您提供发件人视图。

 func tabGesture_Call { let tapRec = UITapGestureRecognizer(target: self, action: "handleTap:") tapRec.delegate = self self.view.addGestureRecognizer(tapRec) //where we want to gesture like: view, label etc } func handleTap(sender: UITapGestureRecognizer) { NSLog("Touch.."); //handling code } 

在迅速它相当简单

在ViewDidLoad()函数中编写此代码

 let tap = UITapGestureRecognizer(target: self, action: #selector(tapHandler(gesture:))) tap.numberOfTapsRequired = 2 tapView.addGestureRecognizer(tap) 

处理程序部分可以在viewDidLoad中,也可以在viewDidLoad之外,将batter放入扩展

 @objc func tapHandler(gesture: UITapGestureRecognizer) { currentGestureStates.text = "Double Tap" } 

在这里,我只是通过打印输出来testing代码,如果你想做一个行动,你可以做任何你想要的或更多的练习和阅读