简体   繁体   English

如何防止UINavigation栏上的同时按钮触摸导致双重动作?

[英]How to prevent double action from simultaneous button touches on UINavigation bar?

In a UINavigation bar, there is a right custom share UIBarButtonItem and a back button in the left UIBarButtonItem. 在UINavigation栏中,右边的自定义共享UIBarButtonItem和左边的UIBarButtonItem中的后退按钮。 When simultaneously tapping on both buttons, the app produces a black view, possibly because both buttons are attempting to display a new view simultaneously - the share button presents a UIActivityViewController and the back button a VC from the prior screen. 当同时点击两个按钮时,该应用程序将产生黑色视图,这可能是因为两个按钮都试图同时显示新视图-共享按钮显示一个UIActivityViewController,后退按钮显示前一屏幕的VC。

In looking through similar questions here, I've tried the following solutions but neither prevented a black view from appearing on a simultaneous button touch: 在浏览此处类似的问题时,我尝试了以下解决方案,但都没有阻止黑屏同时出现在按钮上:

  1. Inserting exclusiveTouch into ViewDidLoad in the following 2 ways 插入exclusiveTouch到viewDidLoad中在以下2种方式

a) for(UIView *temp in self.navigationController.navigationBar.subviews) { [temp setExclusiveTouch:YES]; } a) for(UIView *temp in self.navigationController.navigationBar.subviews) { [temp setExclusiveTouch:YES]; } for(UIView *temp in self.navigationController.navigationBar.subviews) { [temp setExclusiveTouch:YES]; }

b) [self.navigationController.navigationBar setExclusiveTouch:YES]; b) [self.navigationController.navigationBar setExclusiveTouch:YES];

  1. Applying self.navigationController.navigationBar.userInteractionEnabled = NO; 应用self.navigationController.navigationBar.userInteractionEnabled = NO; after a touch. 一触后。

Are there other solutions? 还有其他解决方案吗?

Is this related to multi-threading? 这与多线程有关吗?

In each touch event handler, add the following line: 在每个触摸事件处理程序中,添加以下行:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

When the handler has completed, execute the following: 处理程序完成后,执行以下操作:

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

It's up to you to figure out what to consider the end of the handler. 由您自己确定要考虑处理程序结尾的内容。 If you're pushing or popping view controllers, you might add that second line to the viewWillAppear of the relevant view controllers. 如果要推送或弹出视图控制器,则可以将第二行添加到相关视图控制器的viewWillAppear中。 If you're displaying a modal view controller, you can use the completion handler of -[UIViewController presentViewController:animated:completion:] . 如果要显示模式视图控制器,则可以使用-[UIViewController presentViewController:animated:completion:]的完成处理程序。

Pretty much simple you can use make use of ExclusiveTouch property in this case 在这种情况下,可以使用ExclusiveTouch属性非常简单

[self.navigationController.navigationBar setExclusiveTouch:YES]; [self.navigationController.navigationBar setExclusiveTouch:YES];

This is a Boolean value that indicates whether the receiver handles touch events exclusively. 这是一个布尔值,指示接收方是否专门处理触摸事件。

Setting this property to YES causes the receiver to block the delivery of touch events to other views in the same window. 将此属性设置为YES会导致接收方阻止将触摸事件传递到同一窗口中的其他视图。 The default value of this property is NO. 此属性的默认值为NO。

If you want only one button to respond to touches at a time, you need to set exclusiveTouch for that button, rather than for the parent view. 如果一次只希望一个按钮响应触摸,则需要为该按钮而不是父视图设置exclusiveTouch

Put this just after you add your bar button items. 在添加条形按钮项之后放置此选项。

for(UIView *temp in self.navigationController.navigationBar.subviews)
{
    [temp setExclusiveTouch:YES];
}

or you can set exclusiveTouch individually for each UIBarButton while creating them. 或者您可以设置exclusiveTouch单独为每个UIBarButton在建立它们。

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

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