ios 从 imageview 点击手势获取索引或标签值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17389683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:08:59  来源:igfitidea点击:

get index or tag value from imageview tap gesture

iosobjective-cuiscrollview

提问by Minkle Garg

enter image description here

enter image description here

This is the image from app store whenever we search for any app. I also want to add the same scrollview concept in which it show the currentimage with small preview of previous and next image. I am able to make this view with the help of Sample code. But didn't find any solution that how to get index or tagvalue when user tap any of the image. So that I would be able to open the detail page for each image. If Someone has idea about this please help me.

每当我们搜索任何应用程序时,这是来自应用程序商店的图像。我还想添加相同的滚动视图概念,其中显示当前图像以及上一个和下一个图像的小预览。我可以在示例代码的帮助下创建此视图。但是没有找到任何解决方案,当用户点击任何图像时如何获取索引或标签值。这样我就可以打开每个图像的详细信息页面。如果有人对此有想法,请帮助我。

Thanks in advance.

提前致谢。

回答by SoftDesigner

Add gesture recognizer to the necessary image view:

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

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

Then in the gesture handler get access to the view gesture recognizer attached to:

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

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

回答by iAhmed

At the risk of making a recommendation without seeing the full picture of your app, why not use a custom UIButton instead of UIImageViews for this? With a UIButton you can setup an action and pass the sender id, from which you can easily access your tags and pull the data from your array.

冒着在没有看到应用程序全貌的情况下提出建议的风险,为什么不为此使用自定义 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]; 

And inside of the touchDown: method you simply have to cast the sender to a UIButton in order to access the tag

在 touchDown: 方法中,您只需将发送者强制转换为 UIButton 即可访问标签

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


OR check this out

或者看看这个

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

}

set tag to images as per you want After adding all imageView successfully you get them click like this :-

根据需要将标签设置为图像 成功添加所有 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"]];
     }
}

回答by Uriel

The Best way to achieve what you wish is to add a UITapGesture to your image:

实现您希望的最佳方法是向您的图像添加 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)

Get the superview of your tap gesture, this will give you the correct indexPath. Try this:

获取点击手势的超级视图,这将为您提供正确的 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)
}

You can increase or reduce the superview's depending on you hierarchy level. Start from one superview and keep increasing till you get the indexPath

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

回答by Mundi

Suppose you have an array with the objects to display and a UIImageViewto display the image, just do this when setting up the view with the index i:

假设您有一个包含要显示的对象的数组和一个UIImageView显示图像的数组,只需在使用 index 设置视图时执行以下操作i

imageView.tag = kImageTag + i; 

where kImageTagis any constant > 1 to make sure you don't get a 0 as the tag.

哪里kImageTag是任何常数 > 1 以确保您没有得到 0 作为标签。

Now when the view is selected you can simply check for the tag of the image view.

现在,选择视图时,您只需检查图像视图的标记。

回答by Minkle Garg

I have resolved the issue and really thanks to all to look upon my issue.

我已经解决了这个问题,非常感谢所有关注我的问题。

回答by Nikola Markovic

For those using storyboardsand swift:

对于那些使用storyboardsand 的人swift

  1. Change tag of every UIView or UIImageView in IB (interface builder) ex:

    view1.tag = 1
    view2.tag = 2
    view3.tag = 3
    ...
    
  2. Still in storyboard add UITapGestureRecognizer to every UIView you have put in storyboard view (view1, view2, view3 ...) :
  1. 更改 IB(界面构建器)中每个 UIView 或 UIImageView 的标签,例如:

    view1.tag = 1
    view2.tag = 2
    view3.tag = 3
    ...
    
  2. 仍然在情节提要中将 UITapGestureRecognizer 添加到您放入情节提要视图(view1,view2,view3 ...)的每个 UIView 中:

screenshot of UITapGestureRecognizers (9 in picture)

screenshot of UITapGestureRecognizers (9 in picture)

  1. Write your function in Swift file:
  1. 在 Swift 文件中编写函数:

// code in swift file :

// 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. Connect every TapGestureRecognizer to same function you have declared in parent UIViewController swift file:
  1. 将每个 TapGestureRecognizer 连接到您在父 UIViewController swift 文件中声明的相同函数:

Established connection between UITapGestureRecognizer and func written in swift file

Established connection between UITapGestureRecognizer and func written in swift file

  1. Done.
  1. 完毕。