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

本文详细介绍了如何在iOS应用中实现动态加载等待指示功能,包括使用自定义视图、动态图和提示文字的展示与隐藏,以及如何在请求数据过程中提供等待反馈,以提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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:可以对该类进行封装,方便在请求数据过程中,在界面上显示等待指示,提高用户体验

转载于:https://www.cnblogs.com/chixuedong/p/5243482.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值