如何获取我正在点击的 UIImageView 的标签?
我有几个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
冒着在看不到应用程序全貌的情况下做出推荐的风险,为什么不使用自定义 UIButton 而不是 UIImageViews 呢?使用 UIButton,您可以设置一个操作并传递发件人 ID,从中您可以轻松访问标签并从数组中提取数据。
或者,如果您确实想使用上面的代码并且您知道正在调用 - (void)findOutTheTag:(id)sender 方法,您所要做的就是:
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:
为什么不使用 UIButton 而不是使用 UIImageView?这样你就可以简单地为 UITouchDown 事件添加一个监听器。您可以标记每个按钮,以便在 touchDown 方法中您可以找到按下了哪个按钮。
在 touchDown: 方法内部,您只需将发送者强制转换为 UIButton 即可访问标签。
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.
And inside of the touchDown: method you simply have to cast the sender to a UIButton in order to access the tag.
要找出必须触摸的图像,请使用 touchBegan 方法:
注意: 首先您需要确认图像视图应该是
userIntrectionEnabled=YES;< /代码>
现在使用这个方法:
您可以在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:
You can use switch statement inside touchBegan.