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

陌路散爱 提交于 2020-01-20 14:24:51

问题


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

回答1:


I found these methods very useful:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

[[UIApplication sharedApplication] endIgnoringInteractionEvents];



回答2:


In Swift 3.0 To Disable interaction :-

UIApplication.shared.beginIgnoringInteractionEvents()

To restore interaction :-

UIApplication.shared.endIgnoringInteractionEvents()



回答3:


[_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];

}

}



回答4:


To disable touch event in a view,

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

To enable touch event in a view

[[UIApplication sharedApplication] endIgnoringInteractionEvents];



回答5:


just add

[self.view setUserInteractionEnabled:NO];  

before the

[self.activityIndicator startAnimating];  

and reenable it after

[self.activityIndicator stopAnimating];
[self.view setUserInteractionEnabled:YES]; 



回答6:


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



回答7:


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.




回答8:


@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




回答9:


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.




回答10:


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.



来源:https://stackoverflow.com/questions/5551432/dont-allow-user-interaction-when-activity-indicator-view-is-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!