码迷,mamicode.com
首页 > 移动开发 > 详细

iOS之加载等待指示器(工具类)

时间:2016-03-04 22:36:05      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:

#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之加载等待指示器(工具类)

标签:

原文地址:http://www.cnblogs.com/chixuedong/p/5243482.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!