22

I have a view which contains two views. One of those views contains two buttons and some text labels. The other one, with alpha set to 0.25, has an UIActivityIndicatorView to tell the user that the app is working and he must wait until it finishes. If the user touch a button while the UIActivityIndicatorView is spinning, when the UIActivityIndicatorView stops, the app remember the user action and responds to it. How can I discard the user interaction that occur while the UIActivityIndicatorView is spinning?

Thanks for reading.

P.D.: Like is commented in this thread, I prefer do not to use any modal solution.

EDITED:

I am currently using this code and it does not work right.

- (void)viewDidAppear:(BOOL)animated {

  // The view appears with an UIActivityIndicatorView spinning.
  [self showResults]; // The method that takes a long time to finish.
  [self.activityIndicator stopAnimating];
  // When the showResults method ends, the view shows the buttons to the user.
  [self.activityIndicatorView setHidden:YES];
  [self.menuButton setEnabled:YES];
  [self.menuButton setUserInteractionEnabled:YES];
  [self.playButton setEnabled:YES];
  [self.playButton setUserInteractionEnabled:YES];
  [self.view setUserInteractionEnabled:YES];
  [self.interactionView setUserInteractionEnabled:YES];
}
Community
  • 1
  • 1
Daniel García Baena
  • 1,160
  • 3
  • 19
  • 33
  • this works for me http://stackoverflow.com/questions/5404856/how-to-disable-touch-input-to-all-views-except-the-top-most-view – Sanjin Haracic Oct 18 '12 at 15:12

11 Answers11

113

I found these methods very useful:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

[[UIApplication sharedApplication] endIgnoringInteractionEvents];
Kumar KL
  • 15,086
  • 9
  • 36
  • 57
ppalancica
  • 4,098
  • 4
  • 25
  • 39
7

In Swift 3.0 To Disable interaction :-

UIApplication.shared.beginIgnoringInteractionEvents()

To restore interaction :-

UIApplication.shared.endIgnoringInteractionEvents()
  • If using a function, it's good to add checks like `if UIApplication.shared.isIgnoringInteractionEvents { UIApplication.shared.endIgnoringInteractionEvents() }` Since it seems to be piling up the "begins" so you should add up as many "ends" to avoid having the app locked – Efren Jul 13 '17 at 07:20
4

To disable touch event in a view,

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

To enable touch event in a view

[[UIApplication sharedApplication] endIgnoringInteractionEvents];
Raja Jimsen
  • 224
  • 3
  • 5
4
[_button setUserInteractionEnabled:NO];

That should disable it, just set YES for when you want to user to tap it.

BOOL i_am_ready_to_submit = NO;

-(void)action_finished{

[self.activityIndicator stopAnimating];

i_am_ready_to_submit = YES;

}

-(IBAction)submit_button{

if(i_am_ready_to_submit){

[self submit];

}

}
DavidM
  • 468
  • 1
  • 4
  • 11
  • Check my question, that what I am doing and it does not work right. Where is the problem? – Daniel García Baena Apr 06 '11 at 12:08
  • 1
    Firstly [menubutton setEnabled:NO] will disable the button so you dont need to call userInteraction on it as well. Second if you diable userInteraction on the entire view, you dont need to set it for individual buttons, everything will be disabled. By taking these two points into account you should be able to remove alot of the code from your methods. – DavidM Apr 06 '11 at 13:12
  • I have all this code because I am trying several ways to get what I want. I know that if I disable user interaction into the main view controller, I do not need to disable the buttons or the others views. But even disabling the user interaction into the main view controller (using Interface Builder), enabling again the user interaction into the main view controller when the UIActivityIndicatorView stops, the app remember the user action and responds to it. – Daniel García Baena Apr 06 '11 at 14:52
  • Other comments ont his page pretty much nail it, instead of worrying about disabling buttons and views just put a catch into the code that handles whatever action you need to perform, this can be either if(![self.view activityIndicator isAnimating]){ do stuff } or just a global BOOL if(READY){ do something } that you set yourself when you need it. The button will still respond to the action, but it wont do anything. – DavidM Apr 06 '11 at 14:56
  • Is not doing the snippet of code in the question just what your are saying? If the heavy method has not end, maintaing the user interaction off. When the method finished, enable the user interaction into of the main view controller. Is like check if the spinner is animating or like use a bool variable. – Daniel García Baena Apr 06 '11 at 15:07
  • check my snippet, ignore all the userInteraction stuff, this solution simplifies everything greatly. (IBAction)submit_button is what is called when the button is pressed. – DavidM Apr 06 '11 at 15:19
  • You suggest to maintain the user interaction enabled an to check if the button is clicked when the spinner is spinning. Good idea. I am going to try it. – Daniel García Baena Apr 06 '11 at 15:26
3

just add

[self.view setUserInteractionEnabled:NO];  

before the

[self.activityIndicator startAnimating];  

and reenable it after

[self.activityIndicator stopAnimating];
[self.view setUserInteractionEnabled:YES]; 
Omar Freewan
  • 2,666
  • 4
  • 22
  • 48
1

Use SVProgressHUD WrapperClass It have so many options to show ActivityIndicator
For Source Code Click Here !

[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];


use above statement to disable background touches

[SVProgressHUD dismiss]

To enable background touches.

1
@IBAction func yourButtonPressed(sender: UIButton) {
if self.activityIndicator.isAnimating() {
//remember the action user asked of you using the sender
} else {
//do your stuff
return
}
yourButtonPressed(yourButton)
}

or you code use self.activityIndicator.animationDidStop to determine when to run your stuff

1

You could disable/enable the UIButtons based on the UIActivityIndicatorView being shown or not. Or, if you just want to "discard the user interaction" while the spinner is shown, in the button handler method:

- (void)buttonTapped:(id)sender {
    if ([spinner superview] != nil && [spinner isAnimating]) {
        return;
    }
    // ... the rest of your code
}

This example assumes that when you hide the UIActivityIndicatorView you call one of:

[spinner removeFromSuperview];

or

[spinner stopAnimating];
octy
  • 6,445
  • 1
  • 26
  • 38
0

Though answer is replied in earlier response, just like to add for information purpose "[self.activityIndicatorView setHidden:YES];" no need to call this method explicitly, because startAnimating/stopAnimating already take care of this. I'm assuming you are using default value of "hidesWhenStopped" property.

vipin bansal
  • 650
  • 8
  • 10
0

A quick solution: add a transparent or pseudo transparent view that cover the whole screen. Add your activity indicator on top of this view. When the wait period finishes, remove both views. Get some inspiration.

A better solution, because you can't hide the whole screen in all situations, is to manage the state of the app (ignore actions when the app is 'busy') and disable/enable the appropriate buttons and other controls depending on each app state.

djromero
  • 19,281
  • 4
  • 67
  • 67
  • How can I know if the app is busy or not? – Daniel García Baena Apr 05 '11 at 14:43
  • With 'busy' I mean the period you decide to show the activity indicator. You decide it. For instance, if you're uploading a file, your app is 'busy' until the upload finishes (assuming that you want to lock the UI in that case) – djromero Apr 05 '11 at 17:36
  • I have edited my question with a snippet of code. There I am doing like you say, but it does not work right. Where is the problem? – Daniel García Baena Apr 06 '11 at 11:58
  • Only with that snippet is hard to tell anything else. I think it's better if you just call `[self showResults]` and somewhere else, when results are available call a new method `[self hideActivityIndicators]` or something like that. – djromero Apr 08 '11 at 10:58
0

For swift 5 you can use:

self.view.isUserInteractionEnabled = false

and to enable all again:

self.view.isUserInteractionEnabled = true

If you have some lag problem, like freeze for about a couple of seconds just put this in:

DispatchQueue.main.async{}

clopex
  • 360
  • 5
  • 10