15

在此处输入图像描述

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

提前致谢。

4

8 回答 8

46

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

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);
}
于 2013-06-30T15:34:48.850 回答
4

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

于 2016-01-20T11:58:09.747 回答
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"]];
     }
}
于 2013-06-30T11:36:50.430 回答
1

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

imageView.tag = kImageTag + i; 

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

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

于 2013-06-30T11:26:47.040 回答
1

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

于 2013-07-01T10:49:49.847 回答
1

对于那些使用storyboardsand的人swift

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

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

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. 完毕。
于 2016-01-03T17:54:53.523 回答
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
}
于 2020-12-09T09:48:19.550 回答
0
@IBAction func tapOnRatingInfo(_ sender: UITapGestureRecognizer) {

  guard let index = sender.view?.tag else {
        return
    }
}
于 2021-07-12T10:16:17.687 回答