个人觉得最好用的方式是:
----下面这个是不带文字的-----
@interface SEIndicatorView : UIView
- (void)show;
- (void)hide;
@end
#import "SEIndicatorView.h"
@interface SEIndicatorView()
@property (strong, nonatomic) UIActivityIndicatorView *indicatorView;
@end
@implementation SEIndicatorView
- (id)init
{
self = [super init];
if (self) {
self.frame = [UIScreen mainScreen].bounds;
self.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.5];
_indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
_indicatorView.center = self.center;
[self addSubview:_indicatorView];
[_indicatorView startAnimating];
}
return self;
}
- (void)show
{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[window addSubview:self];
}
- (void)hide
{
[_indicatorView stopAnimating];
[self removeFromSuperview];
}
@end
SEIndicatorView *indicatorView = [[SEIndicatorView alloc] init];
[indicatorView show];
@interface LoadingView : UIView
@property (strong, nonatomic) UIActivityIndicatorView *indicatorView;
@end
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
// Custom a view with an indicator and a label
UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, centerViewSize.width, centerViewSize.height)];
centerView.backgroundColor = [UIColor blackColor];
centerView.layer.cornerRadius = 8.0;
_indicatorView.frame = CGRectMake(centerView.center.x - _indicatorView.frame.size.width/2, 15.0, _indicatorView.frame.size.width, _indicatorView.frame.size.height);
[centerView addSubview:_indicatorView];
UILabel *lblLoading = [[UILabel alloc] initWithFrame:lblLoadingFrame];
lblLoading.text = NSLocalizedString(@"Indicator.Message.Loading", nil);
lblLoading.textColor = [UIColor whiteColor];
lblLoading.font = [UIFont systemFontOfSize:15.0];
lblLoading.backgroundColor = [UIColor clearColor];
lblLoading.textAlignment = NSTextAlignmentCenter;
[centerView addSubview:lblLoading];
centerView.center = self.center;
[self addSubview:centerView];
[_indicatorView startAnimating];
[self setBackgroundColor:[UIColor blackColor]];
[self setAlpha:0.5];
}
return self;
}
@end
_loadingView = [[LoadingView alloc] initWithFrame:self.view.frame];
- (void)startIndicatorAnimation {
[self.view endEditing:YES];
[self.view addSubview:_loadingView];
}
- (void)stopIndicatorAnimation {
sleep(1);
[_loadingView removeFromSuperview];
}