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

时间:2013-06-30 11:02:33

标签: ios objective-c uiscrollview

enter image description here

这是我们搜索任何应用时来自应用商店的图片。我还想添加相同的scrollview概念,在其中显示currentntimage,并预览上一张和下一张图片。我可以在Sample code的帮助下制作此视图。但是没有找到任何解决方案,当用户点击任何图像时如何获得索引或标记值。这样我就可以打开每个图像的详细信息页面。如果有人对此有所了解,请帮助我。

提前致谢。

8 个答案:

答案 0 :(得分:43)

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

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

答案 1 :(得分: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:方法内部,您只需将发件人强制转换为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"]];
     }
}

答案 2 :(得分: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)
}

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

答案 3 :(得分:1)

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

imageView.tag = kImageTag + i; 

其中kImageTag是任何常数&gt; 1确保你没有得到0作为标签。

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

答案 4 :(得分:1)

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

答案 5 :(得分:1)

对于使用storyboardsswift的人:

  1. 在IB(界面构建器)中更改每个UIView或UIImageView的标记ex:

    view1.tag = 1
    view2.tag = 2
    view3.tag = 3
    ...
    
  2. 仍然在故事板中将UITapGestureRecognizer添加到您在故事板视图中放置的每个UIView(view1,view2,view3 ......):
  3. screenshot of UITapGestureRecognizers (9 in picture)

    1. 在Swift文件中编写函数:
    2. // 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文件中声明的相同功能:
      2. Established connection between UITapGestureRecognizer and func written in swift file

        1. 完成。

答案 6 :(得分:0)

迅速

@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
}

答案 7 :(得分:0)

@IBAction func tapOnRatingInfo(_ sender: UITapGestureRecognizer) {

  guard let index = sender.view?.tag else {
        return
    }
}