iOS之加载等待指示器(工具类)
Posted 学东哥哥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS之加载等待指示器(工具类)相关的知识,希望对你有一定的参考价值。
#import "CXDView.h" @interface CXDView () //动态图 @property (strong, nonatomic) UIImageView *loadingImageView; //提示文字 @property (strong, nonatomic) UILabel *toastLabel; @end @implementation CXDView #pragma mark - 初始化 - (instancetype)init { self = [super init]; if (self) { [self addSubview:self.loadingImageView]; [self addSubview:self.toastLabel]; } return self; } #pragma mark - 接口方法 //要把loading界面加载到哪个界面上 + (void)showCXDViewFromSuperView:(UIView *)superView { [self showCXDViewFromSuperView:superView offsetY:0]; } //要把loading界面从哪个界面上移除 + (void)removeCXDViewFromSuperView:(UIView *)superView { //在父视图的【所有子视图数组】当中查找 for (UIView *itemView in superView.subviews) { //如果某个子视图是CXDView类型 if ([itemView isKindOfClass:[CXDView class]]) { //将它从父视图中移除 [itemView removeFromSuperview]; } } } //要把loading界面加载到哪个界面上(具体位置多少) + (void)showCXDViewFromSuperView:(UIView *)superView offsetY:(CGFloat)offsetY { CXDView *loadingView = [[CXDView alloc] init]; loadingView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-40, [UIScreen mainScreen].bounds.size.height/2-40+offsetY, 80, 60); // PS:由于imageView和label不可操作,且自定义View背景为透明,所以无影响 // pss:如果控件可操作,超出view范围,则不可以继续操作 //判断superView上是否已经存在一个CXDView //如果已经存在,那么先删除这个CXDView [self removeCXDViewFromSuperView:superView]; //再加载新的View [superView addSubview:loadingView]; //让动态图动起来 [loadingView.loadingImageView startAnimating]; } #pragma mark - 懒加载 -(UIImageView *)loadingImageView { if (!_loadingImageView) { _loadingImageView = [[UIImageView alloc] init]; _loadingImageView.frame = CGRectMake(0, 0, 80, 80); _loadingImageView.backgroundColor = [UIColor clearColor]; //设置动态图属性 _loadingImageView.animationImages = [self getImageArray]; _loadingImageView.animationDuration = 2.0; _loadingImageView.animationRepeatCount = 0; } return _loadingImageView; } //获取图片数组 - (NSArray *)getImageArray { //获取图片名称 NSMutableArray *imageNameArr = [NSMutableArray array]; for (int i = 1; i < 16; i++) { NSString *imageName; if (i<10) { imageName = [NSString stringWithFormat:@"loading_animate_0%d",i]; }else{ imageName = [NSString stringWithFormat:@"loading_animate_%d",i]; } [imageNameArr addObject:imageName]; } //获取图片数组 NSMutableArray *imageArr = [NSMutableArray array]; for (int i = 0; i < 15; i++) { NSString *imageName = [imageNameArr objectAtIndex:i]; UIImage *image = [UIImage imageNamed:imageName]; [imageArr addObject:image]; } return imageArr; } -(UILabel *)toastLabel { if (!_toastLabel) { _toastLabel = [[UILabel alloc] init]; _toastLabel.frame = CGRectMake(0, 90, 80, 30); _toastLabel.text = @"片刻即来..."; _toastLabel.textColor = [UIColor darkGrayColor]; _toastLabel.font = [UIFont systemFontOfSize:14]; _toastLabel.textAlignment = NSTextAlignmentCenter; } return _toastLabel; } @end
PS:图片文件需要自己添加
PSS:可以对该类进行封装,方便在请求数据过程中,在界面上显示等待指示,提高用户体验
以上是关于iOS之加载等待指示器(工具类)的主要内容,如果未能解决你的问题,请参考以下文章
iOS之深入解析CocoaPods的插件机制和如何加载插件整合开发工具