Don't allow user interaction when activity indicator view is visible

33,223

Solution 1

I found these methods very useful:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

Solution 2

In Swift 3.0 To Disable interaction :-

UIApplication.shared.beginIgnoringInteractionEvents()

To restore interaction :-

UIApplication.shared.endIgnoringInteractionEvents()

Solution 3

To disable touch event in a view,

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

To enable touch event in a view

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

Solution 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];

}

}

Solution 5

just add

[self.view setUserInteractionEnabled:NO];  

before the

[self.activityIndicator startAnimating];  

and reenable it after

[self.activityIndicator stopAnimating];
[self.view setUserInteractionEnabled:YES]; 
Share:
33,223

Related videos on Youtube

Daniel García Baena
Author by

Daniel García Baena

Updated on July 09, 2022

Comments

  • Daniel García Baena
    Daniel García Baena almost 2 years

    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];
    }
    
  • Daniel García Baena
    Daniel García Baena about 13 years
    How can I know if the app is busy or not?
  • djromero
    djromero about 13 years
    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)
  • Daniel García Baena
    Daniel García Baena about 13 years
    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
    Daniel García Baena about 13 years
    Check my question, that what I am doing and it does not work right. Where is the problem?
  • DavidM
    DavidM about 13 years
    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.
  • Daniel García Baena
    Daniel García Baena about 13 years
    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.
  • DavidM
    DavidM about 13 years
    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.
  • Daniel García Baena
    Daniel García Baena about 13 years
    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.
  • DavidM
    DavidM about 13 years
    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.
  • Daniel García Baena
    Daniel García Baena about 13 years
    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.
  • djromero
    djromero about 13 years
    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.
  • coolcool1994
    coolcool1994 almost 11 years
    this didn't prevent user to click back button ""push back
  • Eugene
    Eugene about 10 years
    This is just perfect, it also doesn't dismiss the keyboard if it's visible.
  • bofredo
    bofredo over 8 years
    this kind of answer is a comment
  • Efren
    Efren almost 7 years
    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
  • KSR
    KSR almost 6 years
    Helped me so much