如何获取我正在点击的UIImageView的标签?

flu*_*ang 6 tags iphone uigesturerecognizer

我有几个UIImageView,每个都有一个标签; 我有一个图像数组,我想要做的是:当用户点击其中一个UIImageView时,应用程序从数组中返回特定的图像.

我这样实现:

- (void)viewDidLoad 
{
    [super viewDidLoad];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    [self.view addSubview:scroll];

    NSInteger i;
    for (i=0; i<8; i++) 
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*100 + i*15, 300, 100)];
        imageView.backgroundColor = [UIColor blueColor];
        imageView.userInteractionEnabled = YES;
        imageView.tag = i;

        NSLog(@"%d", imageView.tag);

        [scroll addSubview:imageView];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(findOutTheTag:)];
        [imageView addGestureRecognizer:tap];

    }

    scroll.contentSize = CGSizeMake(320, 115*i);

}
- (void)findOutTheTag:(id)sender
{

    //  HOW TO FIND THE tag OF THE imageView I'M TAPPING?

}
Run Code Online (Sandbox Code Playgroud)

我想找出来imageView.tag,并传递imageView.tag

UIImageView *tappedImage = [imageArray objectAtIndex:imageView.tag];
Run Code Online (Sandbox Code Playgroud)

显示图像.

我确实标记了所有这些,问题是如何找出tag我正在点击的imageView?谢谢你阅读^ _ ^

Rog*_*Rog 12

如果没有看到应用程序的完整图片而有可能提出建议,为什么不为此使用自定义UIButton而不是UIImageViews?使用UIButton,您可以设置操作并传递发件人ID,您可以从中轻松访问您的代码并从阵列中提取数据.

或者,如果你真的想使用上面的代码并且你知道 - (void)findOutTheTag:(id)发送方法被调用,你所要做的就是:

- (void)findOutTheTag:(id)sender {
    switch (((UIGestureRecognizer *)sender).view.tag)      
{
    case kTag1:
    //...
    case kTag2:
    //...
  }
}
Run Code Online (Sandbox Code Playgroud)

  • +1 - (void)childTapped:(UITapGestureRecognizer*)tapGesture {int tag = tapGesture.view.tag;} //为我工作 (3认同)