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

发布于 2024-10-07 14:15:26 字数 1246 浏览 0 评论 0原文

我有几个 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 ?感谢您的阅读^_^

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 Image from array.

i implement like this:

- (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?

}

I want find out the imageView.tag, and pass imageView.tag to

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

to display the image.

I did tag all of them, question is how I can find out the tag of the imageView I'm tapping? thank you for reading ^_^

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

真心难拥有 2024-10-14 14:15:26

冒着在看不到应用程序全貌的情况下做出推荐的风险,为什么不使用自定义 UIButton 而不是 UIImageViews 呢?使用 UIButton,您可以设置一个操作并传递发件人 ID,从中您可以轻松访问标签并从数组中提取数据。

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

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

At the risk of making a recommendation without seeing the full picture of your app, why not use a custom UIButton instead of UIImageViews for this? With a UIButton you can setup an action and pass the sender id, from which you can easily access your tags and pull the data from your array.

OR if you really want to use the above code and your know for a fact the - (void)findOutTheTag:(id)sender method is being called, all your have to do is:

- (void)findOutTheTag:(id)sender {
    switch (((UIGestureRecognizer *)sender).view.tag)      
{
    case kTag1:
    //...
    case kTag2:
    //...
  }
}
別甾虛僞 2024-10-14 14:15:26

为什么不使用 UIButton 而不是使用 UIImageView?这样你就可以简单地为 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: 方法内部,您只需将发送者强制转换为 UIButton 即可访问标签。

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

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
    }
}
鹿港巷口少年归 2024-10-14 14:15:26

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

注意: 首先您需要确认图像视图应该是 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语句。

To find out which image has to be touch ,use touchBegan method:

Note: First of all you need to confirm about image view should be userIntrectionEnabled=YES;
Now use this method:

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

You can use switch statement inside touchBegan.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文