小轮子:loading

2017-12-20  本文已影响0人  dc630f46ee2d
#import <UIKit/UIKit.h>

@interface DishLoadingView : UIView
+ (DishLoadingView *)sharedInstance;
- (void)show;
- (void)hide;
@end

#import "DishLoadingView.h"
#import "Masonry.h"

@interface DishLoadingView ()
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation DishLoadingView
+ (DishLoadingView *)sharedInstance {
    static DishLoadingView *loadingView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        loadingView = [[DishLoadingView alloc] init];
    });
    return loadingView;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [self addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self);
            make.size.mas_equalTo(CGSizeMake(50, 50));
        }];
        NSMutableArray *loadingImgs = [[NSMutableArray alloc] initWithCapacity:75];
        for (NSInteger i = 1; i <= 74; i++) {
            UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"loading_%02ld", i]];
            [loadingImgs addObject:img];
        }
        imageView.animationImages = loadingImgs;
        imageView.animationDuration = 2;
        imageView.animationRepeatCount = 0;
        self.imageView = imageView;
    }
    return self;
}

- (void)show {
    [self removeFromSuperview];
    UIView *superView = [UIApplication sharedApplication].delegate.window;
    [superView addSubview:self];
    [self mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(superView);
    }];
    [self startAnimation];
}

- (void)startAnimation {
    [self.imageView startAnimating];
}

- (void)stopAnimation {
    [self.imageView stopAnimating];
}

- (void)hide {
    [self stopAnimation];
    [self removeFromSuperview];
}


@end
上一篇下一篇

猜你喜欢

热点阅读