简体   繁体   English

如果将UIButton作为子视图添加到UIImageView,则单击UIButton不起作用

[英]Click on UIButton Does Not Work if it's Added as Subview to UIImageView

I have a simple UIImageView that is animated, and i need to detect a tap on it. 我有一个动画的简单UIImageView ,我需要检测它的轻敲。 I've added a UIButton that is a subview of this UIImageView . 我添加了一个UIButton ,它是此UIImageView的子视图。 However,the tap on it does not work. 但是,点击它不起作用。 I've already tested the UIButton itself when added to the view,and it works, so it's not the button that is causing the problem. 添加到视图中时,我已经测试了UIButton本身,并且它可以工作,所以不是导致问题的按钮。

Here's the code: 这是代码:

self.red = [[UIImageView alloc] initWithFrame:CGRectMake(50, 300, 85, 100)];
self.red.image = [UIImage imageNamed:@"red.png"];

[self.red setUserInteractionEnabled: YES];
[self.view addSubview: self.red];
[self.view bringSubviewToFront:self.red]; 

UIButton *redButton = [UIButton buttonWithType: UIButtonTypeCustom];
redButton.frame = CGRectMake(self.red.frame.origin.x, self.red.frame.origin.y, self.red.frame.size.width, self.red.frame.size.height);
[redButton addTarget: self action:@selector(correctButtonClick) forControlEvents: UIControlEventAllTouchEvents];
[redButton setUserInteractionEnabled:YES];

[self.red addSubview: redButton];
[self.red bringSubviewToFront:redButton]; 

What am I doing wrong here? 我在这里做错了什么?

Instead of this line , redButton.frame = CGRectMake(self.red.frame.origin.x, self.red.frame.origin.y, self.red.frame.size.width, self.red.frame.size.height); 代替此行, redButton.frame = CGRectMake(self.red.frame.origin.x, self.red.frame.origin.y, self.red.frame.size.width, self.red.frame.size.height); try this 尝试这个

redButton.frame = CGRectMake(0,0, self.red.frame.size.width, self.red.frame.size.height); 

EDIT : This line [self.red bringSubviewToFront:redButton]; 编辑:此行[self.red bringSubviewToFront:redButton]; is no needed.. 不需要。

Instead of inserting a UIButton as a subview use a UITapGestureRecognizer , for example: 代替将UIButton作为子视图插入,请使用UITapGestureRecognizer ,例如:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(correctButtonClick)];
[self.red addGestureRecognizer:tap];

What you have to do is to invert your implementation, adding UIButton to UIImageView is not a good idea, rather create a custom styled UIButton programatically, and add UIImageView as its subview, would definitely work 您要做的是反转实现,将UIButton添加到UIImageView中不是一个好主意,而是以编程方式创建自定义样式的UIButton并将UIImageView添加为其子视图,这样肯定可以

EDIT: As for you current implementation, youve added UIButton inside a UIImageView so probably you want every part of image to accept the click event, in that case your x and y coords of UIButton would be 0,0 rather than red.x and red.y, however width and height are correct 编辑:至于您当前的实现,您已经在UIImageView中添加了UIButton,所以您可能希望图像的每个部分都接受click事件,在这种情况下,UIButton的x和y坐标将是0,0而不是red.x和red .y,但是宽度和高度正确

Just do self.red.userInteractionEnabled = YES. 只需执行self.red.userInteractionEnabled = YES。

UIImageView has userInteractionEnabled NO as default. UIImageView的userInteractionEnabled默认为NO。

The custom button has no UI. 自定义按钮没有UI。 So you can't select it. 因此您无法选择它。 (you can try it with bound button , or add some image to button and set button alpha to 0.1 or else) (您可以使用绑定按钮尝试,或向按钮添加一些图像并将按钮的alpha设置为0.1或其他)

@tkanzakic answer should be correct. @tkanzakic答案应该是正确的。 Just add TapGestureRecognizer to ImageView or add Image to Button. 只需将TapGestureRecognizer添加到ImageView或将图像添加到Button。

Instead of using separate imageview and Button, make use of the imageview property within the Button . 除了使用单独的imageview和Button之外,还可以使用 Button 中的imageview属性 Which will solve all your problems and relieves you from writing extra lines of code. 这将解决您所有的问题,并使您不必编写额外的代码行。

UIButton *redButton = [UIButton buttonWithType: UIButtonTypeCustom];

redButton.imageView.image = [UIImage imageNamed:@"red.png"]; redButton.imageView.image = [UIImage imageNamed:@“ red.png”];

redButton.frame = CGRectMake(50, 300, 85, 100);
[redButton addTarget: self action:@selector(correctButtonClick) forControlEvents: UIControlEventAllTouchEvents];
[redButton setUserInteractionEnabled:YES];

[self.red addSubview: redButton];

It's simple, the button doesn't respond because its frame is wrong and so it's not where you think it is. 很简单,按钮没有响应,因为它的框架不正确,所以它不在您认为的位置。 Basically change your line below: 基本上在下面更改您的行:

redButton.frame = CGRectMake(self.red.frame.origin.x, self.red.frame.origin.y, self.red.frame.size.width, self.red.frame.size.height);

with this: 有了这个:

redButton.frame = CGRectMake(self.red.bounds.origin.x, self.red.bounds.origin.y, self.red.frame.size.width, self.red.frame.size.height);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM