【问题标题】:get index or tag value from imageview tap gesture从 imageview 点击手势获取索引或标签值
【发布时间】:2013-06-27 16:49:51
【问题描述】:

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

提前致谢。

【问题讨论】:

  • 我认为最好使用UICollectionView 来实现这一点,并为每个水平滚动的项目设置一个单元格。点击每个单元格将导致该项目的详细屏幕/视图。这是UICollectionView的教程:raywenderlich.com/22324/…

标签: ios objective-c uiscrollview


【解决方案1】:

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

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);
}

【讨论】:

  • 我有一个问题,抱歉我来晚了。当我这样做时,发生了一些奇怪的事情。出于某种原因,它总是使用此代码 NSLogs '2'。我将它连接到 4 个 UIView,无论我按下哪一个,它总是 NSLogs '2'。救命!?!?
  • 哦,这很奇怪。我将代码从UIView *view = sender.view; 更改为UIView *view = (UIView *)sender.view;,现在它只打印'3'。怎么回事?!??!
  • 非常有用的解决方案! :)
  • 谢谢。这对我有帮助! :) 应该选择这个作为正确答案。
  • 非常有用的解决方案
【解决方案2】:

实现您想要的最佳方式是在您的图像中添加一个 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)
}

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

【讨论】:

    【解决方案3】:

    冒着在没有看到应用全貌的情况下提出建议的风险,为什么不使用自定义 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: 方法中,您只需将 sender 强制转换为 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"]];
         }
    }
    

    【讨论】:

    • 它不会像我需要的那样显示视图@Emi_believer
    • 好的。然后告诉我你到底想要什么。比如1--这个,2--这个
    • 我已经更新了答案,看看这个。如果它对你有帮助,请把我的答案标记为绿色,干杯
    【解决方案4】:

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

    imageView.tag = kImageTag + i; 
    

    其中kImageTag 是任何常量 > 1 以确保您不会将 0 作为标记。

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

    【讨论】:

    • 我无法在给定的示例中实现这一点。如果可能的话,你可以检查示例代码吗?
    【解决方案5】:

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

    【讨论】:

      【解决方案6】:

      对于那些使用storyboardsswift 的人:

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

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

      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 文件中声明的相同函数:

      1. 完成。

      【讨论】:

        【解决方案7】:

        斯威夫特

        @objc func tabAction(_ sender: YourView){
           /*
           Its weird, sender is not type of `YourView` it is `UITabGestureRecognizer`.
           You can get `YourView` reference from gesture
           */
           let tabedView = sender.view as? YourView
        }
        

        【讨论】:

          【解决方案8】:
          @IBAction func tapOnRatingInfo(_ sender: UITapGestureRecognizer) {
          
            guard let index = sender.view?.tag else {
                  return
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2023-03-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多