Iphone 如何在UIImageView上创建可单击的UIButton?
我想创建一个UI按钮,在UIImageView上显示。我的代码片段如下所示:Iphone 如何在UIImageView上创建可单击的UIButton?,iphone,ios,ipad,uiimageview,uibutton,Iphone,Ios,Ipad,Uiimageview,Uibutton,我想创建一个UI按钮,在UIImageView上显示。我的代码片段如下所示: imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image imageView.userInteractionEnabled = YES; //to enable touch interaction with image UIButton *btn = [UIButton buttonWithType:UIButton
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];
我的应用程序将按钮精确地显示在我想要显示的图像部分,但它不响应单击图像。相反,当我记录其检测的输出时,它会检测单个抽头。但是我想点击这个按钮来选择并执行一些功能
提前谢谢
wahib如果是可点击的图像,我更喜欢图像而不是按钮
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];
您可以只使用一个按钮,然后说:
[UIButton setImage:[imageNamed:@"image.png"]];
我在我的一个应用程序中做了类似的事情。我创建了一个包含一系列图像的xib文件,每个图像的顶部都有一个按钮。我将它们连接到类文件中相应的插座。然后,我计算出触控中点击的内容,并开始使用:
if(CGRectContainsPoint(btnFirstButton.frame, touchLocation)){
//do something useful
}else if(CGRectContainsPoint(btnSecondButton.frame, touchLocation)){
//do something useful
}
希望对我有所帮助:-)我能够在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的子视图或作为ImageView父视图的ScrollView。如果我将Scrollview的按钮子视图设置为[self addSubView:btn];它显示在正确的位置,可以单击,但问题是它是在队列中的所有图像上创建的,因为我将它作为父视图(即滚动视图)的子视图。否则,如果我将其设置为子视图的子视图,即ImageView,它对于每个图像都是唯一的,但仍然显示在所有图像上,并且不可单击:/
any1可以指导我如何使其可点击,并保持动态按钮和图像之间的链接唯一,以便我在队列中每个图像的不同位置都有不同的按钮 我试过这个,它对我有效
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];
您需要将按钮添加为 滚动视图 不是 图像视图 。。 如果按钮是uiimageview的子类,则无法单击。。 为每个按钮指定一个特殊标记,以识别单击的按钮 希望这有帮助
尝试将按钮的用户交互设置为启用
[btn setEnabled:YES];
因为UIImageView将默认设置禁用按钮的用户交互
否则你可以试试这个。只需改变最后两行
替换:
[imageView addSubview:btn];
[self addSubview:imageView];
为此:
[self addSubview:imageView];
[self addSubview:btn];
确保如我所提到的,首先添加imageview,然后添加按钮。否则按钮将被放置在imageview后面,并且看起来是隐藏的 谢谢你的回复。它是否是图像按钮对我来说并不重要。我只想让它可以点击。我现在有两个选择。我是否将我的按钮设置为ImageView的子视图或作为ImageView父视图的ScrollView。如果我将Scrollview的按钮子视图设置为[self addSubView:btn];它显示在正确的位置,可以单击,但问题是它是在队列中的所有图像上创建的,因为我将它作为父视图(即滚动视图)的子视图。否则,如果我将其设置为子视图的子视图,即对于每个图像都是唯一的ImageView,则无法单击:/n您的解决方案似乎最优雅如果您在按钮上设置图像,为什么仍要使用UIImageView?