Swift: How to Disable User Interaction While Touch Action Is Being Carried Out

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

How to temporarily disable user interaction from all textfields if one is being edited?

Create outlet collection for all the textfields like

@IBOutlet var textFs:[UITextField]! 

Then set the vc as the delegate for all of them in viewDidLoad

textFs.forEach { $0.delegate = self }

Implement

func textFieldDidBeginEditing(_ textField: UITextField) { 
textFs.forEach {
if $0 != textField {
$0.isUserInteractionEnabled = false
}
}
}

func textFieldDidEndEditing(_ textField: UITextField) {
textFs.forEach { $0.isUserInteractionEnabled = true }
}

How to disable button touch at the start in Swift

An update with swift 2 and IOS 10 is self.touch.isEnabled = false.

They have gotten rid of enabled and replaced it with isEnabled.

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

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.

Only allow touch after a certain time

You can disable user interaction while clicking on the screen and enable it after 10 second as-

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

for touch in touches {

let location = touch.location(in: self)
player.position.x = location.x
player.position.y = -300//location.y

}
self.view.isUserInteractionEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
self.view.isUserInteractionEnabled = true
}
}

If you want to disable user interaction for 10 seconds after clicking the start button for the first time, then copy this code

self.view.isUserInteractionEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
self.view.isUserInteractionEnabled = true
}

and paste it inside your Start button action function.

Disable user interaction on superview when subView is loaded

Add the tap gestures to the view where you are adding the dropdown view

   tapgesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rmovetheoverlay:)];
tapgesture_.delegate=self;
tapgesture_.enabled=YES;
[<viewwhereaddeddropdown> addGestureRecognizer:mTapGestureRecognizer_];

- (void)rmovetheoverlay:(UITapGestureRecognizer *) gestureRecognizer{

}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
DLog(@"touch view %@",[touch.view superview]);
DLog(@"touch view %@",touch.view);
if (([touch.view isKindOfClass:[UISearchBar class]] && touch.view==mSearchBar_)||[[touch.view superview] isKindOfClass:[UITableViewCell class]]) {
// prevent recognizing touches on the dropdowntable
return NO;
}

return YES;
}


Related Topics



Leave a reply



Submit