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

心不动则不痛 提交于 2019-11-29 22:54:45
ppalancica

I found these methods very useful:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

In Swift 3.0 To Disable interaction :-

UIApplication.shared.beginIgnoringInteractionEvents()

To restore interaction :-

UIApplication.shared.endIgnoringInteractionEvents()
[_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];

}

}

To disable touch event in a view,

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

To enable touch event in a view

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

just add

[self.view setUserInteractionEnabled:NO];  

before the

[self.activityIndicator startAnimating];  

and reenable it after

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

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

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.

@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

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.

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.

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