【问题标题】:how to get the tag of the UIImageView I'm tapping?如何获取我正在点击的 UIImageView 的标签?
【发布时间】:2010-12-13 00:50:32
【问题描述】:

我有几个 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?

}

我想找出imageView.tag,并将imageView.tag 传递给

UIImageView *tappedImage = [imageArray objectAtIndex:imageView.tag];

显示图像。

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

【问题讨论】:

    标签: iphone tags uigesturerecognizer


    【解决方案1】:

    冒着在没有看到应用全貌的情况下提出建议的风险,为什么不使用自定义 UIButton 而不是 UIImageViews 呢?使用 UIButton,您可以设置操作并传递发送者 ID,您可以从中轻松访问标签并从数组中提取数据。

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

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

    【讨论】:

    • 这里的发送者是手势识别器,所以你实际上必须这样做switch (((UIGestureRecognizer *)sender).view.tag)
    • +1 -(void)childTapped:(UITapGestureRecognizer *)tapGesture {int tag = tapGesture.view.tag;} // 为我工作
    【解决方案2】:

    为什么不使用 UIImageView 而不是使用 UIButton。这样你就可以简单地为 UITouchDown 事件添加一个监听器。您可以标记每个按钮,以便在您的 touchDown 方法中找到按下了哪个按钮。

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

    【讨论】:

    • 谢谢你,DHamrick,我尝试了 UIButton 而不是 UIImageView,它完美地输出了标签,确实帮助我跳出了自己的想法,但是使用 UIButton 可能会导致实现很多手势来实现其他我需要使用长按和滑动等手势来移动/删除 viewItem(也涉及标签和数组),它可能在我的构造中变得复杂,因为 UIGestureRecognizer 已经有了它们,我决定使用它们,无论如何,非常感谢你的帮助,我会把应用程序发送给你。 ^_^
    【解决方案3】:

    要找出必须触摸的图像,请使用 touchBegan 方法:

    注意:首先你需要确认一下image view应该是userIntrectionEnabled=YES; 现在使用这个方法:

    -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
        // get touch event
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint touchLocation = [touch locationInView:self.view];
        if ([touch view].tag == 800) {
    
         //if image tag mated then perform this action      
        }
    }
    

    您可以在 touchBegan 中使用 switch 语句。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 2017-05-02
      • 1970-01-01
      相关资源
      最近更新 更多