网络加载的实现+系统菊花的使用

本文介绍了一个用于iOS应用中网络请求加载界面的封装方法。通过创建一个自定义的LoadingView类,实现了加载界面的显示与隐藏功能,并且可以方便地在各种ViewController中调用。文章详细解释了如何设置加载界面的样式、位置以及提示文字。

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

这里写图片描述

网络缓慢时候,通常会有一个网络加载的界面,如图所示

一、加载界面的封装

//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  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值