从imageview轻拍手势获取索引或标记值

在这里输入图像说明

无论何时我们search任何应用,这都是来自app store的图片。 我也想添加相同的滚动视图的概念,其中显示currentimage与前一个和下一个图像的小预览。 我可以在示例代码的帮助下制作这个视图。 但是,当用户点击任何图像时,没有find任何解决scheme来获取索引或tag值。 这样我就可以打开每个图像的详细信息页面。 如果有人有这个想法,请帮助我。

提前致谢。

将手势识别器添加到必要的图像视图中:

 UITapGestureRecognizer *frameTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(frameTapGesture:)]; [_imgView addGestureRecognizer:frameTapGesture]; 

然后在手势处理程序获取访问视图手势识别器附加到:

 - (void)frameTapGesture:(UITapGestureRecognizer*)sender { UIView *view = sender.view; //cast pointer to the derived class if needed NSLog(@"%d", view.tag); } 

如果没有看到您的应用程序的全貌,提出build议的风险,为什么不使用自定义的UIButton而不是UIImageViews呢? 使用UIButton,您可以设置一个操作并传递发件人ID,从中您可以轻松访问您的标签并从数组中提取数据。

  UIButton *button = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*100 + i*15, 300, 100)]; button.backgroundColor = [UIColor blueColor]; button.tag = i; [button addTarget:self action:@selector(touchDown:) controlEvent:UIControlEventTouchDown]; 

而在touchDown:方法中,你只需要将发送者转换为一个UIButton来访问标签

 - (void)touchDown:(id)sender { UIButton* button = (UIButton*)sender; switch(button.tag) { case TAG1: break; //etc } } 

或者检查一下

 UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame: CGRectMake (0,100,200,30)]; NSMutableArray = *images = [NSMutableArray alloc] initWithObjects: img1,img2,img3,nil]; for (int i=0; i<3; i++) { // set `imageView` frame UIImageView *imageV = [[UIImageView alloc]initWithFrame:yourFrame]; [imageV setImage:[images objectAtIndex:i]]; [imageV setUserInteractionEnabled:YES]; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector (getTag:)]; [imageV addGestureRecognizer: singleTap]; [myScroll addSubview:imageV]; } 

设置标签图像按照你想要的所有imageView成功添加后,他们点击这样: –

 -(void)getTag:(id)sender { UIGestureRecognizer *recognizer = (UIGestureRecognizer*)sender; UIImageView *imageView = (UIImageView *)recognizer.view; if(imageView.image.tag==1) { [imageView setImage:[UIImage imageNamed:@"anyImage.png"]]; } } 

假设你有一个数组要显示的对象和一个UIImageView来显示图像,只需要用索引i设置视图时执行此操作:

 imageView.tag = kImageTag + i; 

其中kImageTag是任何常数> 1,以确保你没有得到0作为标签。

现在当select视图时,您可以简单地检查图像视图的标记。

我已经解决了这个问题,真的非常感谢大家看看我的问题。

最好的方法来实现你想要的是添加一个UITapGesture到你的图像:

 let tapgesture = UITapGestureRecognizer(target: self, action: Selector("openImage:")) tapgesture.numberOfTapsRequired = 1 //if you're using a collectionView or tableview add this code inside cellForItemAtIndexPath cell.Img.addGestureRecognizer(tapgesture) //otherwise myImage..addGestureRecognizer(tapgesture) 

获取你的水龙头手势的超级视图,这将给你正确的indexPath。 尝试这个:

 func openImage(sender: UITapGestureRecognizer) { let point = sender.view let mainCell = point?.superview let main = mainCell?.superview let cell: myViewCell = main as! myViewCell let indexPath = collectionView.indexPathForCell(cell) } 

您可以根据您的层次结构级别增加或减less超级视图。 从一个超级视图开始,不断增加,直到你获得indexPath

对于那些使用storyboardsswift

  1. 在IB(界面生成器)中更改每个UIView或UIImageView的标签例如:

     view1.tag = 1 view2.tag = 2 view3.tag = 3 ... 
  2. 在故事板中,将UITapGestureRecognizer添加到放置在故事板视图(view1,view2,view3 …)中的每个UIView中:

UITapGestureRecognizers(9图片)的截图

  1. 在Swift文件中写下你的函数:

//在swift文件中的代码:

  @IBAction func viewTapped(sender: AnyObject) { // You can write anything here. I'm guessing you need something with 'enable/disable' function, and the best call is the CustomView which you create from File > New file print("tapped view is view with tag: \(sender.view!!.tag)") } 
  1. 将每个TapGestureRecognizer连接到您在父UIViewController swift文件中声明的相同函数:

建立了UITapGestureRecognizer和用swift文件写的func之间的连接

  1. 完成。