网络缓慢时候,通常会有一个网络加载的界面,如图所示
一、加载界面的封装
//LoadingView.h @property (nonatomic,strong) UILabel *tipLab; @property (nonatomic,strong) UIActivityIndicatorView *activity; - (void)startAmazing; - (void)stopAmazing; //LoadingView.m - (instancetype)initWithFrame:(CGRect)frame { if (self=[super initWithFrame:frame]) { self.backgroundColor = [UIColor whiteColor]; self.hidden=YES; [self createUI]; } return self; } - (void)createUI { //系统菊花的初始化 _activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray ]; _activity.centerX = self.centerX; _activity.centerY = self.centerY-120; [self addSubview:_activity]; _tipLab = [[UILabel alloc] init]; _tipLab.frame = CGRectMake(0, _activity.bottom+20, ScreenWidth, 16); _tipLab.text = @"努力加载中,请稍后..."; _tipLab.hidden=YES; _tipLab.textColor = H_GRAYTEXTCOLOR; _tipLab.textAlignment = NSTextAlignmentCenter; _tipLab.font = H_FONTCONTENTNAME; [self addSubview:_tipLab]; } - (void)startAmazing { self.hidden=NO; _tipLab.hidden=NO; //菊花开始 [_activity startAnimating]; } - (void)stopAmazing { self.hidden=YES; _tipLab.hidden=YES; //菊花结束 [_activity stopAnimating]; }二、在BaseViewController的基类中初始化
_loadingV = [[LoadingView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)]; //将_loadingV添加到controller的最顶层 _loadingV.layer.zPosition = 10; [self.view addSubview:_loadingV];三、各个ControllView的调用
//在网络请求的方法中添加 - (void)downLoadData{ [self.loadingV startAmazing]; 网络请求:success{ [self.loadV stopAmazing]; } failure{ [self.loadV stopAmazing]; } }四、菊花的属性 《1》初始化方法
- (instancetype)initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style; - (instancetype)initWithFrame:(CGRect)frame; //其中的第一个初始化方法参数style是个枚举类型。 typedef NS_ENUM(NSInteger, UIActivityIndicatorViewStyle) { UIActivityIndicatorViewStyleWhiteLarge, //大小是(37,37) UIActivityIndicatorViewStyleWhite, //大小是(22,22)白色 UIActivityIndicatorViewStyleGray, //大小是(22,22)灰色 };《2》公开方法
- (void)startAnimating; //开启动画,也就是开始旋转。 - (void)stopAnimating; //停止动画,旋转。 - (BOOL)isAnimating; //获取状态 ,0 NO 表示正在旋转,1 YES 表示没有旋转。《3》公开属性
// 默认是UIActivityIndicatorViewStyleWhite @property(nonatomic) UIActivityIndicatorViewStyle activityIndicatorViewStyle; // 默认是 YES. 设置动画结束是否隐藏控件。 @property(nonatomic) BOOL hidesWhenStopped; //设置菊花的颜色 @property (nullable, readwrite, nonatomic, strong) UIColor *color //菊花的背景颜色 @property (nonatomic) UIColor *backgroundColor