iOS开发(OC)——一句话搞定加载等待动画

本文介绍了如何通过优化处理,在网络请求快速完成时避免加载页面的闪现,确保至少0.5秒的显示时间,并提供了相应的.h和.m文件代码示例,以及GitHub上的demo地址和iOS开发交流群信息。

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

(因为有加载的图片,所以想要看到最完整的效果,请点击 https://github.com/liumude/LoadingView 下载 iOS开发交流群:301058503(如果不是开发者,做广告的就不用进了,因为迟早都被踢))

**

此加载页做了优化处理:在网络状态良好的情况下(1秒请求完毕),不会显示,防止闪现。 有可能是1.1秒请求完毕,那样也会闪现,这里也做了处理,在显示加载页之后,至少0.5秒后才会隐藏(如果在显示之后的0.5秒内请求完毕,则显示0.5秒隐藏,如果在显示之后的0.5秒后请求完毕,则请求完毕之后隐藏)

**

  1. .h文件
#import <UIKit/UIKit.h>

typedef enum : NSUInteger {
    RequestLoading,
    RequestSuccess,
    RequestFaild,
} RequestResult;

@interface JZLoadingViewPacket : UIView


- (void)showWithTitle:(NSString *)title result:(RequestResult)result;

- (void)jz_hide;

+ (JZLoadingViewPacket *)shareInstance;


+ (void)showWithTitle:(NSString *)title result:(RequestResult)result addToView:(UIView *)selfView;

@end
  1. .m文件
#import "JZLoadingViewPacket.h"

@interface JZLoadingViewPacket ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *bgView;


//控制显示后再隐藏的时间间隔>=1秒,提高用户体验
@property (nonatomic, assign) BOOL isShow;
@property (nonatomic, assign) BOOL isUseHiden;

@end

@implementation JZLoadingViewPacket


- (void)showWithTitle:(NSString *)title result:(RequestResult)result  {
    _isShow = NO;
    _isUseHiden = NO;
    self.hidden = NO;
    self.backgroundColor = [UIColor clearColor];
    
    self.titleLabel.text = title;

    if (result == RequestLoading) {
        self.imageView.image = [UIImage imageNamed:@"rotationImage"];
    }else if (result == RequestSuccess) {
        self.imageView.image = [UIImage imageNamed:@"requestSuccess"];
    }else if (result == RequestFaild) {
        self.imageView.image = [UIImage imageNamed:@"networkFaild"];
    }

    [self.imageView.layer removeAllAnimations];
    if (result == RequestLoading) {

        self.bgView.hidden = YES;
        self.imageView.hidden = YES;
        self.titleLabel.hidden = YES;
        [self performSelector:@selector(jz_show) withObject:nil afterDelay:1.0];
        
        CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"opacity"];
        //设置动画完成后保持原状
        animation1.fillMode = kCAFillModeForwards;
        anima
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值