iOS如何实现逐帧动画做loading视图
短信预约 -IT技能 免费直播动态提醒
这篇文章给大家分享的是有关iOS如何实现逐帧动画做loading视图的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
具体内容如下
我封装了一个可复用的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; - (void)setImageArrayByName:(NSString *)name andExtName:(NSString *)extName andCount:(int)count; @end
.m文件
#import "FZImageSequenceLoadingView.h" @implementation FZImageSequenceLoadingView @synthesize errorImage;@synthesize errorMsg;@synthesize imageArray;@synthesize loadingMsg;@synthesize timerInterval; - (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { timerInterval = 1; currentImageIndex = -1; } return self;} - (void)setupSubviews {// self.backgroundColor = [UIColor redColor]; if (self.imageArray && self.imageArray.count > 0) { if (!_imageView) { _imageView = [[UIImageView alloc] init]; [self addSubview:_imageView]; } //让图片view为本view的顶部居中,大小为图片数组中的第一张 UIImage *firstImg = [self.imageArray objectAtIndex:0]; _imageView.size = firstImg.size; _imageView.top = 0; _imageView.left = (self.size.width - _imageView.size.width) / 2; } if (self.loadingMsg) { CGSize labelSize = [self.loadingMsg sizeWithFont:[UIFont systemFontOfSize:11]]; if (!_lblMsg) { _lblMsg = [[UILabel alloc] initWithFrame:CGRectZero]; _lblMsg.textAlignment = NSTextAlignmentCenter; [self addSubview:_lblMsg]; } _lblMsg.font = [UIFont systemFontOfSize:11]; _lblMsg.size = labelSize; _lblMsg.textColor = [UIColor darkGrayColor]; _lblMsg.backgroundColor = [UIColor clearColor]; _lblMsg.bottom = self.height; _lblMsg.left = (self.width - _lblMsg.width) / 2; }} - (void)switchToStatus:(FZImageSequenceLoadingStatus)status { if (!_lblMsg || !_imageView) { [self setupSubviews]; } switch (status) { case FZImageSequenceLoadingStatusError: [self switchToError]; break; case FZImageSequenceLoadingStatusLoading: [self switchToLoading]; break; case FZImageSequenceLoadingStatusStop: [self switchToStop]; break; }} - (void)switchToStop { [timer invalidate]; timer = nil; if (self.imageArray && self.imageArray.count > 0) { _imageView.image = [self.imageArray objectAtIndex:0]; }} - (void)switchToError { [timer invalidate]; timer = nil; //如果有错误状态的图 if (self.errorImage) { _imageView.image = self.errorImage; //如果没有就用第一张动画图 } else if (self.imageArray && self.imageArray.count > 0) { _imageView.image = [self.imageArray objectAtIndex:0]; } if (self.errorMsg) { _lblMsg.text = self.errorMsg; }} - (void)switchToLoading { if (self.loadingMsg) { _lblMsg.text = self.loadingMsg; } if (!timer) { timer = [NSTimer scheduledTimerWithTimeInterval:self.timerInterval target:self selector:@selector(showNextImage) userInfo:nil repeats:YES]; }} - (void)showNextImage { if (!imageArray || imageArray.count < 1) { return; } currentImageIndex = (currentImageIndex + 1) % self.imageArray.count; // 主线程执行: dispatch_async(dispatch_get_main_queue(), ^{ _imageView.image = [imageArray objectAtIndex:currentImageIndex]; });} - (void)setImageArrayByName:(NSString *)name andExtName:(NSString *)extName andCount:(int)count { NSAssert((name && extName && (count > 0)), @"图片名字和数量错误"); NSMutableArray *imgs = [NSMutableArray arrayWithCapacity:count]; for (int i = 1; i <= count; i++) { NSString *imgName = [NSString stringWithFormat:@"%@_%i%@", name, i, extName]; UIImage *image = [UIImage imageNamed:imgName]; NSLog(@"%@", image); if (!image) { continue; } [imgs addObject:image]; } self.imageArray = imgs;} @end
使用示例,在uiwebview中使用如下:
初始化视图:
//设置loading视图- (void)setupLoadingView { if (!_loadingView) { _loadingView = [[FZImageSequenceLoadingView alloc] initWithFrame:CGRectMake(0, 0, 170, 70)]; _loadingView.center = self.view.center; [_loadingView setImageArrayByName:@"loading" andExtName:@".png" andCount:10]; _loadingView.loadingMsg = @"努力加载中,请稍候"; _loadingView.errorMsg = @"加载失败"; _loadingView.timerInterval = 0.1; _loadingView.hidden = YES; [self.view addSubview:_loadingView]; }}
在uiwebview的代理方法中切换状态:
#pragma mark - webview delegate- (void)webViewDidStartLoad:(UIWebView *)webView { if (_loadingView.hidden) { _loadingView.hidden = NO; [_loadingView switchToStatus:FZImageSequenceLoadingStatusLoading]; }} - (void)webViewDidFinishLoad:(UIWebView *)webView { if (!_loadingView.hidden) { [_loadingView switchToStatus:FZImageSequenceLoadingStatusStop]; _loadingView.hidden = YES; } }- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(@"load page error:%@", [error description]); if (!_loadingView.hidden) { [_loadingView switchToStatus:FZImageSequenceLoadingStatusError]; }}
目前该组件功能还不够完善,但是能满足目前我自己的需求,后续再继续丰富。
感谢各位的阅读!关于“iOS如何实现逐帧动画做loading视图”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341