How to Disable User Interaction on Swiftui View

How to disable interaction while ProgressView showing (SwiftUI)

You can use the .disabled modifier on your controls.

For example:

Button("Button") { //action }.disabled(isLoading)

(You may want some sort convince property like this: private var isLoading : Bool { viewModel.state == .loading } )

In fact, because SwiftUI view modifiers like this will apply down to child views, you can apply it to a whole stack or group of controls:

VStack {
//controls in here
}.disabled(isLoading)

Disabling user interaction of the current view on screen

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

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

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

same for Swift:

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

and Swift 3/4

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

edit for iOS 13:
beginIgnoringInteractionEvents is deprecated in iOS13

just make a new full size View and lay it over your current view.
that will allow you to block any user interaction.

How can I block user interactions with the background views while modal transitions popup shows in swiftUI?

You can add .disabled(Bool) in your background view with a @State Boolean variable. As-

struct HomePageSwiftUIView: View {
@State private var isModalShowing: Bool = false
var body: some View {
VStack() {
// Your background view
}
.disabled(isModalShowing)
VStack() {
// Your background view
}
.disabled(isModalShowing)
VStack() {
// Your popup view (Dont add it)
}
}
}

While clicking alert button just set isModalShowing = true and also isModalShowing = false when alert dismissed.

Disabling user interaction in entire screen

Since the picker is added to window's view , you can do this in the currentVC

self.view.isUserInteractionEnabled =  // true/false

also as you have a navigation , you may also do

self.navigationController?.navigationBar.isUserInteractionEnabled =  // true/false

Swift: how to disable user interaction while touch action is being carried out?

Try to get the view from the touch object and then dissable the user interaction on it.

touch.view.isUserInteractionEnabled = false

Disable user interaction in some part of screen

Suppose your side menu's view hierarchy is setup like this:

View #1
|_ title label
|_ button
|_ button

You can now embed it inside another view, that'll be invisible:

View #0
|_ View #1
|_ title label
|_ button
|_ button

...where view #1 would be your regular side menu view that covers, say, 70% of the screen's width.

Now, set the view #0's background color to .clear.

Also, change your constraint logic to move the side menu to the screen's edge.

This will give you a side menu that covers the full screen, disables taps outside it and looks like it only covers part of the screen.


Extra Credit:

Add a UITapGestureRecognizer to view #0. When triggered, you can dismiss the side menu.

How to disable multi touch on entire app or just a view using SwiftUI?

No SwiftUI solution but you can use UIKit solution in SwiftUI app.

Use UIView.appearance(). This will disable multi-touch on the whole app.

@main
struct SwiftUIDEMO: App {
init() {
UIView.appearance().isMultipleTouchEnabled = false
UIView.appearance().isExclusiveTouch = true
}

// Body View
}

Or you can also use UIViewController touch event and maange .isUserInteractionEnabled.

extension UIViewController {
open override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
self.view.isUserInteractionEnabled = true
}
open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
self.view.isUserInteractionEnabled = true
}
open override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
self.view.isUserInteractionEnabled = true
}
}


Related Topics



Leave a reply



Submit