我封装了一个可复用的loading视图组件,用于按照一定周期逐帧播放加载动画。代码如下:
.h文件
#import <UIKit/UIKit.h>
//加载状态
typedef enum {
FZImageSequenceLoadingStatusStop = 1, // 停止
FZImageSequenceLoadingStatusLoading, // 加载中
FZImageSequenceLoadingStatusError //发生错误
} FZImageSequenceLoadingStatus;
@interface FZImageSequenceLoadingView : UIView {
UIImageView *_imageView;
UILabel *_lblMsg;
NSTimer *timer;
int currentImageIndex;
}
@property(strong) NSArray *imageArray; //动画序列的图片数组
@property(strong, nonatomic) UIImage *errorImage;
@property(nonatomic, strong) NSString *errorMsg;
@property(nonatomic, strong) NSString *loadingMsg; //提示文字
@property(nonatomic) CGRect imageFrame; //图片的Frame
@property(nonatomic) CGRect msgFrame; //文字内容的Frame
@property(nonatomic) float timerInterval; //切换图片的周期
/**
切换状态
*/
- (void)switchToStatus:(FZImageSequenceLoadingStatus)status;
/**
通过图片名字和数量设置图片数组,如给定名字"name"、“.png”和数量4,则会去加载“name_1.png”到"name_4.png"的图片
*/
- (void)setImageArrayByName:(NSString *)name andExtName:(NSString *)extName andCount:(int)count;
@end
.m文件
#import "FZImageSequenceLoadingView.h"
@implementation FZImageSequenceLoadingView
@synthesize errorImage