1

我在这个网站上得到了这个解决方案:点击一个 UIImage 并在 Objective-c 中打开一个 UIImageView

添加UITapGestureRecognizer到您的UIImageView

UITapGestureRecognizer *tapRecognizer;
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourSelector)];
[thumbnail addGestureRecognizer:tapRecognizer];
[tapRecognizer release];

thumbnail.userInteractionEnabled = YES; // very important for UIImageView

这对于单个 ImageView 来说工作得很好,但是我在我的 scrollView 中添加了多个(大约 20 个)然后我如何区分哪个 ImageView 将被用户点击或选择。我试图设置我自己的@selector(imageClicked),但它只返回最后一个 imageView 的标签。

我在循环中添加 addGestureRecognizer,因为我在 imageView 中动态加载 20 个静态图像。

4

5 回答 5

5

这可能会有所帮助

for(int i=0;i<20;i++)
{
    UIImageView *img=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yourimage.png"]];
    [img setTag:i];
    img.frame= //set frame accordingly;
    img.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [img addGestureRecognizer:tap];
    [tap release];
    [scrollView addSubView:img];
}

- (void)handleTap:(UITapGestureRecognizer *)recognizer  {
    UIImageView *imageView = (UIImageView *)recognizer.view;

      switch([imageView tag])
    {
       case 1:
          //do your work
          break;
       .
       .
       .
       .
       case n:

    }
}
于 2012-01-31T07:48:48.523 回答
1
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
for (UIImageView *thumbnail in imageArray)  {
    [thumbnail addGestureRecognizer:tapRecognizer];
}
[tapRecognizer release];

您可以从 UIGestureRecognizer 的“视图”属性中获取视图。例如,在您的选择器中:

- (void)handleTap:(UITapGestureRecognizer *)recognizer  {
    UIImageView *imageView = (UIImageView *)recognizer.view;

    //  Now do something with your view
}
于 2012-01-31T07:16:58.003 回答
1

您不能将单击识别器添加到多个视图。为每个要添加点击识别器的视图创建一个新视图。由于您使用的是表格视图,因此只需在tableView:cellForRowAtIndexPath:方法中执行此操作:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // usual implementation
    static NSString *cellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeue ...];
    if (!cell) {
        cell = [[UITableViewCell alloc] init....];
        // add new gesture recognizer here.
    }

    // setup cell: set the image (just an example)
    cell.imageView.image = [images objectAtIndex:indexPath.row];

    return cell;
}

而不是使用其他答案中提到的标签,而只是让 imageview 尝试使用底层模型。在处理点击时,找到 indexPath 以了解要访问的模型对象:

- (void)handleTap:(UITapGestureRecognizer *)recognizer  {
    UIImageView *imageView = (UIImageView *)recognizer.view;

    // assumes the image view is direct subview of the cell
    // change to match your cell structure
    UITableViewCell *cell = (UITableViewCell *) [imageView superview]; 

    // get the index path for the cell clicked
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    //  TODO: Use index path to get full image to display
}

这样您就可以知道所单击图像的确切行,因此您可以访问您的模型以访问要显示的完整图像。

于 2012-01-31T08:06:54.840 回答
0

您需要为所有图像添加标签 - 例如 thumbnail.tag = 100。然后将您的选择器修改为 youSelector:(UITapGestureRecognizer *)sender; 在选择器中添加开关

- (void) yourSelector:(UITapGestureRecognizer *)sender {
    UIImageView *imageView = (UIImageView *)sender.view;
    switch(imageView.tag) {
        case 100: {
        //This code will be handled if tag == 100;
        }
    }
}
于 2012-01-31T07:23:33.370 回答
0

请尝试将 ImageView 子类化并将手势识别器添加到子类中。

现在为每个图像创建 ImageView 对象并将图像添加到该对象。设置一些独特的属性,以便您可以识别像图像名称一样单击的对象。

于 2012-01-31T08:59:42.027 回答