前言
在开发中,App加载数据的时候想显示一个加载的动画。这样可以使App更美观,更高大上一些。下面就开始自定义一个这样的控件吧。直接贴代码吧
效果图
-
准备组合动画的图片
-
.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