美文网首页iOS技术点
iOS自定义加载动画控件

iOS自定义加载动画控件

作者: Torin76 | 来源:发表于2017-09-27 12:41 被阅读426次

前言

在开发中,App加载数据的时候想显示一个加载的动画。这样可以使App更美观,更高大上一些。下面就开始自定义一个这样的控件吧。直接贴代码吧

效果图

QQ20170927-123406-HD.gif
  • 准备组合动画的图片

Screenshot 2017-09-27_12-39-30.png
  • .h文件

#import <UIKit/UIKit.h>

@interface TLLoadingAnimationView : UIView

//展示动画
- (void)showInView:(UIView *)view;

//取消动画
- (void)dismiss;

@end
  • .m文件

#import "TLLoadingAnimationView.h"

@interface  TLLoadingAnimationView ()
@property (nonatomic,weak) UIImageView *imageView;
@property (nonatomic,strong) NSMutableArray *imageArray;
@end

@implementation TLLoadingAnimationView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0.94f green:0.94f  blue:0.94f  alpha:1.00f];
    }
    
    return  self;
}

- (void)showInView:(UIView *)view {
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    
    [view addSubview:self];
    self.frame = view.bounds;
    self.imageView.frame  = CGRectMake(0, 0, 70, 100);
    self.imageView.center = self.center;
    
    [self.imageView startAnimating];
}


- (void)dismiss {
    [_imageArray removeAllObjects];
    [_imageView stopAnimating];
    [_imageView removeFromSuperview];
    [self removeFromSuperview];
}

- (NSMutableArray *)imageArray {
    
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return  _imageArray;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        
        UIImageView *img = [[UIImageView alloc]init];
        [self addSubview:img];
        _imageView = img;
        for (NSInteger i = 1; i < 5; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading_640x1136_%ld", i]];
            [self.imageArray addObject:image];
        }
        self.imageView.animationDuration = 1.0;
        self.imageView.animationRepeatCount = 0;
        self.imageView.animationImages = self.imageArray;
    }
      return _imageView;
    
}

@end

相关文章

网友评论

    本文标题:iOS自定义加载动画控件

    本文链接:https://www.haomeiwen.com/subject/sqquextx.html