如何在 UIImageView 上制作可点击的 UIButton?
我想创建一个 UIButton 以显示在 UIImageView 上。我的代码片段如下所示:
imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"ClickMe" forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont systemFontOfSize:16]];
btn.frame = CGRectMake(340, 550, 70, 50);
[btn addTarget:self action:@selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
[imageView addSubview:btn];
[self addSubview:imageView];
我的应用程序在我想要的图像部分精确显示按钮,但它不响应单击图像。相反,当我记录其检测输出时,它确实检测到单击。但我希望在选择时单击此按钮并执行某些功能。
提前致谢。
瓦希卜
I want to create a UIButton to display over an UIImageView. My code snippet is as shown below:
imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"ClickMe" forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont systemFontOfSize:16]];
btn.frame = CGRectMake(340, 550, 70, 50);
[btn addTarget:self action:@selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
[imageView addSubview:btn];
[self addSubview:imageView];
My application displays button exactly on part of image where i want to but it doesn't responds to click image. Instead it does detect single tap as i log output on its detection. But i want this button to get clicked on selection and to perform some function.
Thanks in advance.
wahib
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我尝试过这个,它对我有用......
i tried this and it works for me.......
您只需使用一个按钮即可说出:
You can use just one button and say:
您必须添加自定义按钮。它将为您工作。
UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
you have to add define button as custom.It will work for you.
UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
您需要将按钮添加为 a 的子类
不
..
如果按钮是 uiimageview 的子类,则它将不可单击..
为每个按钮分配一个特殊标签以识别单击的按钮
希望这有帮助
you need to add the button as a subclass of a
not
..
it wont be clickable if the button is a subclass of a uiimageview ..
assign a speacial tag for each button to recognize the clicked button
hope this helps
尝试将按钮的用户交互设置为启用。
因为 UIImageView 会将按钮的用户交互设置为默认禁用。
否则你可以尝试这个。只需更改最后两行即可。
将:替换
为:
确保首先添加图像视图,然后添加按钮,正如我所提到的。否则按钮将被放置在图像视图后面并且看起来是隐藏的。
try setting the userinteraction of the button enabled.
Coz the UIImageView will set the userinteraction of the button disabled by default.
Else you could try this.Just change the last two lines.
Replace:
with this:
Make sure you add the imageview first and the button second as i have mentioned. else the button would b placed behind the imageview and would seems hidden.
如果它是可点击的图像,我更喜欢图像而不是按钮。
If it is clickable image I prefer image over button.
我在我的一个应用程序中做了类似的事情。我创建了一个 xib 文件,其中包含一系列图像,每个图像的顶部都有一个按钮。我将它们连接到类文件中相应的插座。然后我找出了在 TouchesBegan 中单击的内容:
希望有帮助 - 对我有用:-)
I did something like this in one of my apps. I created a xib file that contained a series of images, each with a button drawn over the top. I connected those to the corresponding outlets in the class file. I then worked out which was clicked in touchesBegan using:
Hope that helps - worked for me :-)
我能够在 UIImageVIew 上显示 UIButton,这是更新代码。我在底部提到了最近的问题。
我现在有两个选择。我是否将按钮设置为 ImageView 的子视图或 ScrollView(它是 ImageView 的父视图)。如果我制作 Scrollview 的按钮子视图,例如 [self addSubView:btn];它显示在正确的位置并且可单击,但问题是它是在队列中的所有图像上创建的,因为我将其设为父视图(即滚动视图)的子视图。否则,如果我将其设置为子视图的子视图,即 ImageView,它对于每个图像都是唯一的,但仍然显示在所有图像上并且不可单击:/
any1 可以指导我如何使其可单击并保持动态按钮和图像是唯一的,因此我在队列中每个图像的不同位置都有不同的按钮。
I am able to show UIButton on UIImageVIew and this is the update code. I have mentioned the recent problems at bottom.
I have two choices rite now. Whether i make my button a subview of ImageView or ScrollView which is parent of ImageView. If i make button subview of Scrollview like [self addSubView:btn]; it displays at right position and is clickable but problem is that it is created on all images in the queue because i am making it a subview of parent view i.e scrollview. Else if i make it subview of child view i.e ImageView which is unique for every image, but still its shown on all images and its not clickable :/
Can any1 guide me on how to make it clickable and keeping the linkage between dynamic button and the image unique so that i have different buttons at various positions on each image in the queue.