ios 当活动指示器视图可见时不允许用户交互
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5551432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Don't allow user interaction when activity indicator view is visible
提问by Daniel García Baena
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?
我有一个包含两个视图的视图。其中一个视图包含两个按钮和一些文本标签。另一个,alpha 设置为 0.25,有一个UIActivityIndicatorView
告诉用户应用程序正在运行,他必须等到它完成。如果用户在UIActivityIndicatorView
旋转时触摸按钮,当UIActivityIndicatorView
停止时,应用程序会记住用户的操作并做出响应。如何丢弃UIActivityIndicatorView
旋转时发生的用户交互?
Thanks for reading.
谢谢阅读。
P.D.: Like is commented in this thread, I prefer do not to use any modal solution.
PD:喜欢在这个线程中评论,我更喜欢不使用任何模态解决方案。
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];
}
回答by ppalancica
I found these methods very useful:
我发现这些方法非常有用:
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
回答by Shailendra Suriyal
In Swift 3.0 To Disable interaction :-
在 Swift 3.0 中禁用交互:-
UIApplication.shared.beginIgnoringInteractionEvents()
To restore interaction :-
恢复互动:-
UIApplication.shared.endIgnoringInteractionEvents()
回答by DavidM
[_button setUserInteractionEnabled:NO];
That should disable it, just set YES for when you want to user to tap it.
那应该禁用它,只需在您希望用户点击它时设置为 YES。
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];
}
}
回答by Raja Jimsen
To disable touch event in a view,
要在视图中禁用触摸事件,
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
To enable touch event in a view
在视图中启用触摸事件
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
回答by Omar Freewan
just add
只需添加
[self.view setUserInteractionEnabled:NO];
before the
之前
[self.activityIndicator startAnimating];
and reenable it after
并在之后重新启用它
[self.activityIndicator stopAnimating];
[self.view setUserInteractionEnabled:YES];
回答by octy
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:
您可以根据UIActivityIndicatorView
显示与否禁用/启用 UIButton 。或者,如果您只想在显示微调器时“放弃用户交互”,请在按钮处理程序方法中:
- (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:
此示例假设当您隐藏时,UIActivityIndicatorView
您会调用以下选项之一:
[spinner removeFromSuperview];
or
或者
[spinner stopAnimating];
回答by Dinesh Reddy Chennuru
Use SVProgressHUD WrapperClass It have so many options to show ActivityIndicator
For Source Code Click Here !
使用 SVProgressHUD WrapperClass 它有很多选项可以显示
源代码的ActivityIndicator
点击这里!
[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
use above statement to disable background touches
使用上面的语句禁用背景触摸
[SVProgressHUD dismiss]
[SVProgressHUD 解散]
To enable background touches.
启用背景触摸。
回答by Mohsen Hossein pour
@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
或者您编写代码使用 self.activityIndicator.animationDidStop 来确定何时运行您的东西
回答by vipin bansal
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.
虽然在前面的回复中回复了答案,只是为了提供信息而添加“[self.activityIndicatorView setHidden:YES];” 无需显式调用此方法,因为 startAnimating/stopAnimating 已经解决了这个问题。我假设您使用的是“hidesWhenStopped”属性的默认值。
回答by djromero
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.
一个更好的解决方案,因为你不能在所有情况下隐藏整个屏幕,是管理应用程序的状态(当应用程序“忙碌”时忽略操作)并根据每个应用程序禁用/启用适当的按钮和其他控件状态。