简体   繁体   English

如果将UIButton放置在两个视图之间,则不会触发动作

[英]UIButton not triggering action if place between two views

I've got a UIButton which is linked to a correspondent @IBAction. 我有一个UIButton链接到对应的@IBAction。 Such button is located exactly in the middle of two adjacent views 该按钮恰好位于两个相邻视图的中间

|--------------|----------------| | view1 | button | view2 | |--------------|----------------|

The UIButton is a subview of view1 (important!), but is located in the middle using a frame with a negative value for the 'x' position. UIButton是view1的子视图(重要!),但位于中间,使用带有x位置负值的框架。

Both views have a UITapGestureRecognizer, used to trigger some stuff. 这两个视图都有一个UITapGestureRecognizer,用于触发某些内容。

The problem is the following: if I tap on the left side of the button (the one over view1) the IBAction is triggered, while if I tap on the right part of the button (the one on view2) the IBAction is not triggered, while the UIGestureRecognizer on view2 is. 问题如下:如果我点击按钮的左侧(在view1上的那个),则触发IBAction,而如果我点击按钮的右侧(在view2上的那个),则不会触发IBAction,而view2上的UIGestureRecognizer是。

I know that this is probably because the button belongs to view1 and not view2, but is there a way to detect this? 我知道这可能是因为按钮属于view1而不是view2,但是有没有办法检测到这个?

I tried with the suggested approach 我尝试了建议的方法

override func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
   /*detect if you tapped button or view*/
}

which is correctly invoked, but always saying that the view tapped is view2. 可以正确调用它,但总是说被点击的视图是view2。

Thanks 谢谢

I suggest you to go with two buttons and make them appear like one, each one on view1 and view2 held closer. 我建议您使用两个按钮,使它们看起来像一个,将view1和view2上的每个按钮保持更近的位置。 If you have a title on the button, then have a label behind the button with the same frame as your current button on view1 and make the two button's background color as clearcolor. 如果按钮上有标题,则在按钮后面有一个标签,该标签与view1上当前按钮的框架相同,并将两个按钮的背景色设置为clearcolor。 Keep the same target and selector for both buttons. 两个按钮的目标和选择器保持相同。 Simple and Easy. 简单又容易。

Make sure that in your storyboard the button is above the views, see the attached screenshot, where the Button is above the textField. 确保在情节提要中,按钮位于视图上方,请参阅附件的屏幕截图,其中按钮位于textField上方。 在此处输入图片说明

Your button is likely being covered by one of your other views, so those views are swallowing the taps. 您的按钮可能已被其他视图之一覆盖,因此这些视图吞没了水龙头。 I suggest running your app and using the debug menu's view debugging option to see the hierarchy of views. 我建议运行您的应用程序并使用调试菜单的视图调试选项来查看视图的层次结构。 I'm not at my Mac right now so I can't give you the specific command, but there is a menu option that pauses your running app and shows a 3d representation of your back-to-front view hierarchy. 我现在不在Mac上,因此无法向您提供特定的命令,但是有一个菜单选项可暂停您正在运行的应用程序并显示从后到前的视图层次结构的3d表示形式。 It's invaluable in figuring out stuff like this. 找出这样的东西是无价的。

If your button is a subview of view1, taps outside of view1's bounds won't register. 如果您的按钮是view1的子视图,则不会注册在view1范围之外的点击。 That's why you can tap on the left side, but not the right. 这就是为什么您可以点击左侧而不是右侧的原因。

It would be better to to make view1, view2 and the button children of the same superview, and make sure the button is added on top of the two views. 最好使view1,view2和同一超级视图的子按钮相同,并确保将按钮添加到两个视图的顶部。

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

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