how to get the tag of the UIImageView I'm tapping?

前端 未结 3 1371
傲寒
傲寒 2020-12-16 05:29

i have several UIImageView, each of them has a tag; and I have an array of Images, what i want to do is: when user tap one of the UIImageView, the app give back the certain

3条回答
  •  轮回少年
    2020-12-16 05:41

    Instead of using a UIImageView why don't you use an UIButton. That way you can simply add a listener for UITouchDown events. You can tag each button so that in your touchDown method you can find which button was pressed.

        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]; 
    

    And inside of the touchDown: method you simply have to cast the sender to a UIButton in order to access the tag.

    - (void)touchDown:(id)sender
    {
        UIButton* button = (UIButton*)sender;
        switch(button.tag)
        {
            case TAG1:
               break;
            //etc
        }
    }
    

提交回复
热议问题