IOS 利用UIImageView实现加载动画(简单封装)

先来看效果图

IOS 利用UIImageView实现加载动画(简单封装)_第1张图片
loading.gif

直接上代码吧,以后用的时候,直接copy就行

DCLoadingAnimationView.h

#import 

NS_ASSUME_NONNULL_BEGIN

@interface DCLoadingAnimationView : UIView

- (void)showInView:(UIView *)view;

- (void)dismiss;
@end

NS_ASSUME_NONNULL_END

DCLoadingAnimationView.m

#import "DCLoadingAnimationView.h"

@interface DCLoadingAnimationView ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *textLabel;
@property (nonatomic, strong) NSMutableArray *imageArray;
@end
@implementation DCLoadingAnimationView
-(void)showInView:(UIView *)view
{
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    [view addSubview:self];
    
    self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    self.backgroundColor = [UIColor whiteColor];
    self.imageView.frame = CGRectMake(0, 0, 80*[UIScreen mainScreen].bounds.size.width/375, 80*[UIScreen mainScreen].bounds.size.width/375);
    self.imageView.center = CGPointMake(self.centerX, self.centerY-64);
    
    self.textLabel.frame = CGRectMake(0, CGRectGetMaxY(self.imageView.frame)+5, self.bounds.size.width, 20);
    
    [self.imageView startAnimating];
}
-(void)dismiss
{
    [self.imageView stopAnimating];
    [self.imageArray removeAllObjects];
    [self.imageView removeFromSuperview];
    [self.textLabel removeFromSuperview];
    [self removeFromSuperview];
}
- (NSMutableArray *)imageArray {
    if (!_imageArray) {
        _imageArray = [NSMutableArray new];
    }
    return _imageArray;
}
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        [self addSubview:_imageView];
        
        for (NSInteger i = 1; i <= 4; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading%zd", i]];
            [self.imageArray addObject:image];
        }
        //图片播放一次所需时长
        _imageView.animationDuration = 1.0;
        
        //图片播放次数,0表示无限
        _imageView.animationRepeatCount = 0;

        //设置动画图片数组
        _imageView.animationImages = self.imageArray;
    }
    return _imageView;
}
- (UILabel *)textLabel
{
    if (!_textLabel) {
        UILabel *textLabel = [[UILabel alloc]init];
        textLabel.text = @"优优正在努力加载...";
        textLabel.textColor = [UIColor colorWithRed:150/255.0 green:150/255.0 blue:150/255.0 alpha:1.0];
        textLabel.font = [UIFont systemFontOfSize:14];
        textLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:textLabel];
        _textLabel = textLabel;
    }
    return _textLabel;
}
@end

写个UIView的分类,UIView+Loading.h

#import 
#import "DCLoadingAnimationView.h"
NS_ASSUME_NONNULL_BEGIN

@interface UIView (Loading)

- (DCLoadingAnimationView *)loadingView;

- (void)showLoadingView;

- (void)hideLoadingView;

@end

NS_ASSUME_NONNULL_END

UIView+Loading.m

#import "UIView+Loading.h"
#import 

const static char loadingViewKey;

@implementation UIView (Loading)

-(void)showLoadingView
{
    if (!self.loadingView) {
        DCLoadingAnimationView *loadingView = [[DCLoadingAnimationView alloc]init];
        [loadingView showInView:self];
        self.loadingView = loadingView;
    }
}
- (void)hideLoadingView
{
    if (self.loadingView) {
        [self.loadingView dismiss];
        self.loadingView = nil;
    }
    
}
-(UIView *)loadingView
{
    return objc_getAssociatedObject(self, &loadingViewKey);
}
-(void)setLoadingView:(UIView *)loadingView{
    objc_setAssociatedObject(self, &loadingViewKey, loadingView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
使用的时候直接调用
[self.view showLoadingView];
反之则
[self.view hideLoadingView];

你可能感兴趣的:(IOS 利用UIImageView实现加载动画(简单封装))