ios 禁用屏幕上当前视图的用户交互

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7878022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:01:53  来源:igfitidea点击:

Disabling user interaction of the current view on screen

iphoneioscocoa-touchipad

提问by Vin

My app has many views and their respective controllers. Now I have a set of model classes with business logic in them. One of the model classes(subclass of NSObject) has the responsibility of managing security. It's intended function is to listen for a particular instruction from a web server and if a 'disable' message arrives from server, disable the UI for any further usage.

我的应用程序有很多视图和它们各自的控制器。现在我有一组带有业务逻辑的模型类。模型类之一(NSObject 的子类)负责管理安全性。它的预期功能是侦听来自 Web 服务器的特定指令,如果“禁用”消息从服务器到达,则禁用 UI 以供进一步使用。

Now the 'disable' message can arrive at any instant during the functioning of the app and any view can be visible on screen. How do I determine which view is visible to the user(from my model class) and disable user interaction for it?

现在,“禁用”消息可以在应用程序运行期间的任何时刻到达,并且可以在屏幕上看到任何视图。如何确定哪个视图对用户可见(从我的模型类)并禁用用户交互?

回答by Gotschi

Maybe you want the whole application to not react at all?

也许您希望整个应用程序根本没有反应?

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

use [[UIApplication sharedApplication] endIgnoringInteractionEvents];to revert this (credits to nerith)

用于[[UIApplication sharedApplication] endIgnoringInteractionEvents];还原此(归功于 nerith)

same for Swift:

Swift 也一样:

UIApplication.sharedApplication().beginIgnoringInteractionEvents()
UIApplication.sharedApplication().endIgnoringInteractionEvents()

and Swift 3/4

和斯威夫特 3/4

UIApplication.shared.beginIgnoringInteractionEvents()
UIApplication.shared.endIgnoringInteractionEvents()

回答by mightyr

Here is the code for Swift 3

这是 Swift 3 的代码

UIApplication.shared.beginIgnoringInteractionEvents() 
UIApplication.shared.endIgnoringInteractionEvents()

Slight update to the syntax

对语法略有更新

回答by Micah Hainline

I've done something very similar to this. I disable all user interaction by placing a translucent black view over everything else, which visually distinguishes the fact that the entire UI is disabled, and blocks all touch events. I usually just add this view to the window class after I've added the view controller's view to the window, and then just hide it when it's not needed.

我做过与此非常相似的事情。我通过在其他所有内容上放置一个半透明的黑色视图来禁用所有用户交互,这在视觉上区分了整个 UI 被禁用的事实,并阻止了所有触摸事件。我通常只是在将视图控制器的视图添加到窗口后将此视图添加到窗口类,然后在不需要时将其隐藏。

回答by Jayprakash Dubey

Use following code for disabling interaction with background

使用以下代码禁用与背景的交互

//Ignore interaction for background activities
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

Now if you want to enable the interaction use following snippet

现在,如果您想启用交互,请使用以下代码段

if ([[UIApplication sharedApplication] isIgnoringInteractionEvents]) {

    // Start interaction with application
    [[UIApplication sharedApplication] endIgnoringInteractionEvents];
}

回答by Zorayr

I am a little hesitant to disable interactions for the entire app - this is too aggressive and too intrusive, what happens when the view controller is inside a split view controller? Then both view controllers will be disabled!

我有点犹豫要禁用整个应用程序的交互 - 这太激进且太具有侵入性,当视图控制器位于拆分视图控制器内时会发生什么?然后两个视图控制器都将被禁用!

Instead, you could create a clear-colored view controller and presented it modally, see example below for Swift 2:

相反,您可以创建一个清晰的视图控制器并以模态方式呈现它,请参阅下面的 Swift 2 示例:

private func TransparentViewController() -> UIViewController {
  let transparentViewController = UIViewController(nibName: nil, bundle: nil)
  transparentViewController.modalPresentationStyle = .OverCurrentContext
  transparentViewController.modalTransitionStyle = .CrossDissolve
  transparentViewController.view.backgroundColor = UIColor.clearColor()
  return transparentViewController
}

And now you can present it from within your view controller, before presenting the HUD:

现在您可以在显示 HUD 之前从您的视图控制器中显示它:

let transparentViewController = TransparentViewController()
self.presentViewController(transparentViewController, animated:false, completion: nil) 

Hope this helps!

希望这可以帮助!

回答by utahwithak

You could add a delegate in the class that is listening to the server and so when it gets that message it just calls disable on whomever its delegate is. Whichever view is showing to get the message as well as normal execution until the message is received. If it is a singleton just set the view as the delegate on viewWillAppear.

您可以在侦听服务器的类中添加一个委托,因此当它收到该消息时,它只会对其委托的任何人调用禁用。无论显示哪个视图以获取消息以及在收到消息之前正常执行。如果它是单例,只需将视图设置为 上的委托viewWillAppear

Another viable option is to use the notification center. So when your class gets the disable message just do

另一个可行的选择是使用通知中心。因此,当您的班级收到禁用消息时,请执行以下操作

[[NSNotificationCenter defaultCenter] postNotificationName:@"disableView" object:nil];

and when your views load add them to listen

并在您的视图加载时添加它们以进行收听

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disableView:) name:@"disableView" object:nil];

Then stop listening when they aren't needed.

然后在不需要它们时停止聆听。

Subclassing UIViewController and implementing the disable functionality and then subclassing that class in all other view controllers would eliminate the duplication of code.

子类化 UIViewController 并实现禁用功能,然后在所有其他视图控制器中子类化该类将消除代码重复。

回答by Huy Vu

Here is the code for Swift 2.2 iOS 9.3

这是 Swift 2.2 iOS 9.3 的代码

UIApplication.sharedApplication().beginIgnoringInteractionEvents()
UIApplication.sharedApplication().endIgnoringInteractionEvents()

Use it frequently, work like champ for me, very useful for a view with many IBActions that make API call and you dont want to make another call in waiting for first response

经常使用它,对我来说就像冠军一样,对于具有许多进行 API 调用的 IBAction 的视图非常有用,并且您不想在等待第一次响应时再次调用