如何检查当前点击的uiimageview?

时间:2016-12-28 09:16:38

标签: ios objective-c uiimageview

我有三个UIImageview。我使用UIImageviewtapGesture设置了操作。我希望每个UIImageview都有不同的动作。如何查看当前点击的UIImageview?

UITapGestureRecognizer *singleTap =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapping:)];
[singleTap setNumberOfTapsRequired:1];
[_imageOne addGestureRecognizer:singleTap];
[_imageTwo addGestureRecognizer:singleTap];
[_imageThree addGestureRecognizer:singleTap];

-(void)singleTapping:(UIGestureRecognizer *)recognizer {
}

3 个答案:

答案 0 :(得分:1)

您可以使用以下代码进行检测:

-(void)singleTapping:(UITapGestureRecognizer *) recognizer{
        if (recognizer.view == _imageOne){

        } else if (recognizer.view == _imageTwo) {

        }else
        {
        }
    }

答案 1 :(得分:0)

UIGestureRecognizer与单个视图一起使用。 Apple documentation中有明确的信息:

  

手势识别器附加到视图

     

每个手势识别器都与一个视图相关联。相比之下,a   视图可以有多个手势识别器,因为单个视图   可能会回应许多不同的手势。对于手势识别器   识别特定视图中发生的触摸,必须附加   这种观点的手势识别器。

答案 2 :(得分:0)

首先,您无法将同一UITapGestureRecognizer附加到多个视图。

因此,您的代码无法运行,您必须创建三个UITapGestureRecognizer实例,并且必须将其附加到以下视图中:

UITapGestureRecognizer *singleTapImg1 =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapping:)];

[singleTapImg1 setNumberOfTapsRequired:1];

UITapGestureRecognizer *singleTapImg2 =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapping:)];

[singleTapImg2 setNumberOfTapsRequired:1];

UITapGestureRecognizer *singleTapImg3 =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapping:)];

[singleTapImg3 setNumberOfTapsRequired:1];

[_imageOne addGestureRecognizer:singleTapImg1];
[_imageTwo addGestureRecognizer:singleTapImg2];
[_imageThree addGestureRecognizer:singleTapImg3];

现在,您可以使用以下代码获取,点击哪个imageview并根据以下内容执行操作:

-(void)singleTapping:(UIGestureRecognizer *)recognizer {

   if (recognizer.view == _imageOne){

       //_imageOne tapped

   } 
   else if (recognizer.view == _imageTwo) {

       //_imageTwo tapped

   }else{

       //_imageThree tapped
   }

}