如何在 UIImageView 上制作可点击的 UIButton?

发布于 2024-12-12 03:11:44 字数 781 浏览 0 评论 0原文

我想创建一个 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 技术交流群。

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

发布评论

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

评论(8

风和你 2024-12-19 03:11:45

我尝试过这个,它对我有用......

UIImageView * imageView = [[UIImageView alloc]init];
[imageView setImage:[UIImage imageNamed:@"image.jpeg"]];
[imageView setFrame:CGRectMake(10,10,300,300)];
[imageView setContentMode:UIViewContentModeScaleToFill];
[imageView setUserInteractionEnabled:TRUE];

UIButton * ButtonOnImageView = [[UIButton alloc]init];
[ButtonOnImageView setFrame:CGRectMake(135,120,60,30)];
[ButtonOnImageView setImage:[UIImage imageNamed:@"image.jpeg"] forState:UIControlStateHighlighted];
 [ButtonOnImageView setImage:[UIImage imageNamed:@"image2.jpeg"] forState:UIControlStateNormal];
[ButtonOnImageView addTarget:self action:@selector(OnClickOfTheButton) forControlEvents:UIControlEventTouchUpInside];

 [imageView addSubview:ButtonOnImageView];
 [self.view addSubview:imageView];

i tried this and it works for me.......

UIImageView * imageView = [[UIImageView alloc]init];
[imageView setImage:[UIImage imageNamed:@"image.jpeg"]];
[imageView setFrame:CGRectMake(10,10,300,300)];
[imageView setContentMode:UIViewContentModeScaleToFill];
[imageView setUserInteractionEnabled:TRUE];

UIButton * ButtonOnImageView = [[UIButton alloc]init];
[ButtonOnImageView setFrame:CGRectMake(135,120,60,30)];
[ButtonOnImageView setImage:[UIImage imageNamed:@"image.jpeg"] forState:UIControlStateHighlighted];
 [ButtonOnImageView setImage:[UIImage imageNamed:@"image2.jpeg"] forState:UIControlStateNormal];
[ButtonOnImageView addTarget:self action:@selector(OnClickOfTheButton) forControlEvents:UIControlEventTouchUpInside];

 [imageView addSubview:ButtonOnImageView];
 [self.view addSubview:imageView];
似最初 2024-12-19 03:11:45

您只需使用一个按钮即可说出:

[UIButton setImage:[imageNamed:@"image.png"]];

You can use just one button and say:

[UIButton setImage:[imageNamed:@"image.png"]];
万水千山粽是情ミ 2024-12-19 03:11:45
  1. 您必须添加自定义按钮。它将为您工作。

    UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];

  1. you have to add define button as custom.It will work for you.

    UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];

倾城泪 2024-12-19 03:11:45

您需要将按钮添加为 a 的子类

滚动视图

图像视图

..
如果按钮是 uiimageview 的子类,则它将不可单击..
为每个按钮分配一个特殊标签以识别单击的按钮

希望这有帮助

you need to add the button as a subclass of a

scrollview

not

imageview

..
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

拧巴小姐 2024-12-19 03:11:45

尝试将按钮的用户交互设置为启用。

[btn setEnabled:YES];

因为 UIImageView 会将按钮的用户交互设置为默认禁用。

否则你可以尝试这个。只需更改最后两行即可。

将:替换

[imageView addSubview:btn];
[self addSubview:imageView]; 

为:

[self addSubview:imageView];
[self addSubview:btn];

确保首先添加图像视图,然后添加按钮,正如我所提到的。否则按钮将被放置在图像视图后面并且看起来是隐藏的。

try setting the userinteraction of the button enabled.

[btn setEnabled:YES];

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:

[imageView addSubview:btn];
[self addSubview:imageView]; 

with this:

[self addSubview:imageView];
[self addSubview:btn];

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.

生生漫 2024-12-19 03:11:45

如果它是可点击的图像,我更喜欢图像而不是按钮。

UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(kLeftMargin, 10, self.view.bounds.size.width - kLeftMargin -      kRightMargin, 52)];
[sampleButton setTitle:@"Button Title" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];

If it is clickable image I prefer image over button.

UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(kLeftMargin, 10, self.view.bounds.size.width - kLeftMargin -      kRightMargin, 52)];
[sampleButton setTitle:@"Button Title" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];
七颜 2024-12-19 03:11:45

我在我的一个应用程序中做了类似的事情。我创建了一个 xib 文件,其中包含一系列图像,每个图像的顶部都有一个按钮。我将它们连接到类文件中相应的插座。然后我找出了在 TouchesBegan 中单击的内容:

if(CGRectContainsPoint(btnFirstButton.frame, touchLocation)){
    //do something useful
}else if(CGRectContainsPoint(btnSecondButton.frame, touchLocation)){
    //do something useful
}

希望有帮助 - 对我有用:-)

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:

if(CGRectContainsPoint(btnFirstButton.frame, touchLocation)){
    //do something useful
}else if(CGRectContainsPoint(btnSecondButton.frame, touchLocation)){
    //do something useful
}

Hope that helps - worked for me :-)

源来凯始玺欢你 2024-12-19 03:11:45

我能够在 UIImageVIew 上显示 UIButton,这是更新代码。我在底部提到了最近的问题。

imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image

//imageView.tag = i; // tag our images for later use when we place them in serial form
[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact
imageView.userInteractionEnabled = YES; //to enable touch interaction with image

[self addSubview:imageView];

UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button

btn.frame = CGRectMake(215, 550, 100, 100);
[btn addTarget:self action:@selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working

**[self addSubview:btn]; //Buttons are clickable but shows button on all images**

//[imageView addSubview:btn]; //Buttons are not clickable and are shown on all images

我现在有两个选择。我是否将按钮设置为 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.

imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image

//imageView.tag = i; // tag our images for later use when we place them in serial form
[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact
imageView.userInteractionEnabled = YES; //to enable touch interaction with image

[self addSubview:imageView];

UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button

btn.frame = CGRectMake(215, 550, 100, 100);
[btn addTarget:self action:@selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working

**[self addSubview:btn]; //Buttons are clickable but shows button on all images**

//[imageView addSubview:btn]; //Buttons are not clickable and are shown on all images

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.

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