如何在点击它时从不同的图像视图中获取图像标签

发布于 2024-10-18 16:31:56 字数 47 浏览 0 评论 0原文

我想使用 nos 中的图像视图。单击任何特定图像视图时,滚动视图中的图像视图数。

I want to used image view from nos. of image views in scroll view on single tapping any particular image view.

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

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

发布评论

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

评论(4

何其悲哀 2024-10-25 16:31:56

如果您坚持使用图像而不是按钮,您可以使用手势识别器。创建一个 imageView 并启用其 userInteraction

UIImageView *testImageView              =   [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage"]];
testImageView.frame                     =   CGRectMake(30.0,30.0,60.0,40.0);
testImageView.tag                       =   30;
testImageView.userInteractionEnabled    =   TRUE;
[tempPlotView addSubview: testImageView];
[testImageView release];    

现在分配一个手势识别器对象并将其添加到您的 imageView...

UITapGestureRecognizer *testGesture =   [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singletap:)];
[testGesture setNumberOfTapsRequired:1];
[testImageView addGestureRecognizer: testGesture];
[testGesture     release];

现在在选择器“singleTap”中您可以执行任何操作...

-(void)singleTap:(UIImageView*)sender{
    if(sender.tag == 30){
        //do your stuff here...
    }
}

希望这有帮助...干杯...

If you are insistent on using images instead of buttons you can use Gesture Recognizer. Create an imageView and enable its userInteraction

UIImageView *testImageView              =   [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage"]];
testImageView.frame                     =   CGRectMake(30.0,30.0,60.0,40.0);
testImageView.tag                       =   30;
testImageView.userInteractionEnabled    =   TRUE;
[tempPlotView addSubview: testImageView];
[testImageView release];    

Now allocate a gesture Recognizer object and add it to your imageView...

UITapGestureRecognizer *testGesture =   [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singletap:)];
[testGesture setNumberOfTapsRequired:1];
[testImageView addGestureRecognizer: testGesture];
[testGesture     release];

Now in the selector "singleTap" you can do whatever your action is..

-(void)singleTap:(UIImageView*)sender{
    if(sender.tag == 30){
        //do your stuff here...
    }
}

Hope this help...cheers....

仙气飘飘 2024-10-25 16:31:56

创建一个按钮并将图像设置为背景。在创建按钮时,您可以设置类似标签

button.tag=yourtag;//your tag integer value 
[button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchDown];

  [button setBackgroundImage:[UIImage imageNamed:@"img.png"] forState:UIControlStateNormal];

并在您的类中实现此功能,

 - (void)buttonTouched:(UIButton *)sender 
   {
NSLog(@"touched %i",[sender tag]);
   }    

同时点击特定按钮此功能将被调用。

Create a button and set image as background.while creating button you can set tag like

button.tag=yourtag;//your tag integer value 
[button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchDown];

  [button setBackgroundImage:[UIImage imageNamed:@"img.png"] forState:UIControlStateNormal];

and implement this function in your clss

 - (void)buttonTouched:(UIButton *)sender 
   {
NSLog(@"touched %i",[sender tag]);
   }    

while tapping the particular button this function will get called.

请爱~陌生人 2024-10-25 16:31:56

您可以使用以图像视图作为内容的 UIButton,而不是使用图像视图。

这样,当通过引用按钮点击给定图像时,您将收到回调。从那里你应该能够获得已被点击的标签!

Rather than use image views, you could use UIButtons with image views as their content.

That way, you'll get a callback when a given image is tapped with a reference to the button. From there you should be able to get the tag of that which has been tapped!

浮生面具三千个 2024-10-25 16:31:56

你可以按照尼克的建议去做。
或者,您可以创建 imageview 的子类。设置框架和标签。
在这个自定义类的touchesEnded方法中,你可以根据它的标签找到它是哪个imageview。

You can do according to what Nick has suggested.
Or else, you can create a subclass of the imageview. Set frame and tag to it.
In touchesEnded method of this custom class, you can find which imageview it is based on its tag.

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