电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

iOS實現(xiàn)逐幀動畫做loading視圖

瀏覽:49日期:2022-09-16 16:09:45

本文實例為大家分享了iOS實現(xiàn)逐幀動畫做loading視圖的具體代碼,供大家參考,具體內(nèi)容如下

我封裝了一個可復(fù)用的loading視圖組件,用于按照一定周期逐幀播放加載動畫。代碼如下:

.h文件

#import <UIKit/UIKit.h> //加載狀態(tài)typedef enum { FZImageSequenceLoadingStatusStop = 1, // 停止 FZImageSequenceLoadingStatusLoading, // 加載中 FZImageSequenceLoadingStatusError //發(fā)生錯誤} FZImageSequenceLoadingStatus; @interface FZImageSequenceLoadingView : UIView { UIImageView *_imageView; UILabel *_lblMsg; NSTimer *timer; int currentImageIndex;} @property(strong) NSArray *imageArray; //動畫序列的圖片數(shù)組 @property(strong, nonatomic) UIImage *errorImage; @property(nonatomic, strong) NSString *errorMsg; @property(nonatomic, strong) NSString *loadingMsg; //提示文字 @property(nonatomic) CGRect imageFrame; //圖片的Frame @property(nonatomic) CGRect msgFrame; //文字內(nèi)容的Frame @property(nonatomic) float timerInterval; //切換圖片的周期 /** 切換狀態(tài) */- (void)switchToStatus:(FZImageSequenceLoadingStatus)status; /** 通過圖片名字和數(shù)量設(shè)置圖片數(shù)組,如給定名字'name'、“.png”和數(shù)量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;@synthesize errorMsg;@synthesize imageArray;@synthesize loadingMsg;@synthesize timerInterval; - (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) {timerInterval = 1;currentImageIndex = -1; } return self;} /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ - (void)setupSubviews {// self.backgroundColor = [UIColor redColor]; if (self.imageArray && self.imageArray.count > 0) {if (!_imageView) { _imageView = [[UIImageView alloc] init]; [self addSubview:_imageView];}//讓圖片view為本view的頂部居中,大小為圖片數(shù)組中的第一張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; //如果有錯誤狀態(tài)的圖 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; // 主線程執(zhí)行: 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)), @'圖片名字和數(shù)量錯誤'); 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中使用如下:

初始化視圖:

//設(shè)置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的代理方法中切換狀態(tài):

#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]; }}

目前該組件功能還不夠完善,但是能滿足目前我自己的需求,后續(xù)再繼續(xù)豐富。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: IOS
相關(guān)文章:
主站蜘蛛池模板: 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 超细|超微气流粉碎机|气流磨|气流分级机|粉体改性机|磨粉机|粉碎设备-山东埃尔派粉体科技 | 健康管理师报名入口,2025年健康管理师考试时间信息网-网站首页 塑料造粒机「厂家直销」-莱州鑫瑞迪机械有限公司 | 网带通过式抛丸机,,网带式打砂机,吊钩式,抛丸机,中山抛丸机生产厂家,江门抛丸机,佛山吊钩式,东莞抛丸机,中山市泰达自动化设备有限公司 | 电子元器件呆滞料_元器件临期库存清仓尾料_尾料优选现货采购处理交易商城 | 高铝砖-高铝耐火球-高铝耐火砖生产厂家-价格【荣盛耐材】 | 苏州注册公司_苏州代理记账_苏州工商注册_苏州代办公司-恒佳财税 | PU树脂_水性聚氨酯树脂_聚氨酯固化剂_聚氨酯树脂厂家_宝景化工 | 防火门|抗爆门|超大门|医疗门|隔声门-上海加汇门业生产厂家 | 红酒招商加盟-葡萄酒加盟-进口红酒代理-青岛枞木酒业有限公司 | 齿辊分级破碎机,高低压压球机,立式双动力磨粉机-郑州长城冶金设备有限公司 | 谈股票-今日股票行情走势分析-牛股推荐排行榜 | 综合管廊模具_生态,阶梯护坡模具_检查井模具制造-致宏模具厂家 | 艺术涂料_进口艺术涂料_艺术涂料加盟_艺术涂料十大品牌 -英国蒙太奇艺术涂料 | 浙江美尔凯特智能厨卫股份有限公司 | 散热器厂家_暖气片_米德尔顿散热器 | 隧道窑炉,隧道窑炉厂家-山东艾瑶国际贸易 | 仿古建筑设计-仿古建筑施工-仿古建筑公司-汉匠古建筑设计院 | 卷筒电缆-拖链电缆-特种柔性扁平电缆定制厂家「上海缆胜」 | 福州仿石漆加盟_福建仿石漆厂家-外墙仿石漆加盟推荐铁壁金钢(福建)新材料科技有限公司有保障 | 内窥镜-工业内窥镜厂家【上海修远仪器仪表有限公司】 | YT保温材料_YT无机保温砂浆_外墙保温材料_南阳银通节能建材高新技术开发有限公司 | 板框压滤机-隔膜压滤机配件生产厂家-陕西华星佳洋装备制造有限公司 | 破碎机锤头_耐磨锤头_合金锤头-鼎成机械一站式耐磨铸件定制服务 微型驱动系统解决方案-深圳市兆威机电股份有限公司 | 钢结构-钢结构厂房-钢结构工程[江苏海逵钢构厂] | 花纹铝板,合金铝卷板,阴极铝板-济南恒诚铝业有限公司 | 除尘器布袋骨架,除尘器滤袋,除尘器骨架,电磁脉冲阀膜片,卸灰阀,螺旋输送机-泊头市天润环保机械设备有限公司 | 黑龙江「京科脑康」医院-哈尔滨失眠医院_哈尔滨治疗抑郁症医院_哈尔滨精神心理医院 | 重庆钣金加工厂家首页-专业定做监控电视墙_操作台 | 河南正规膏药生产厂家-膏药贴牌-膏药代加工-修康药业集团官网 | 恒压供水控制柜|无负压|一体化泵站控制柜|PLC远程调试|MCGS触摸屏|自动控制方案-联致自控设备 | 恒压供水控制柜|无负压|一体化泵站控制柜|PLC远程调试|MCGS触摸屏|自动控制方案-联致自控设备 | SPC工作站-连杆综合检具-表盘气动量仪-内孔缺陷检测仪-杭州朗多检测仪器有限公司 | 知企服务-企业综合服务(ZiKeys.com)-品优低价、种类齐全、过程管理透明、速度快捷高效、放心服务,知企专家! | 清洁设备_洗地机/扫地机厂家_全自动洗地机_橙犀清洁设备官网 | 防伪溯源|防窜货|微信二维码营销|兆信_行业内领先的防伪防窜货数字化营销解决方案供应商 | 手持式线材张力计-套帽式风量罩-深圳市欧亚精密仪器有限公司 | 超声波清洗机_超声波清洗机设备_超声波清洗机厂家_鼎泰恒胜 | 精密五金加工厂-CNC数控车床加工_冲压件|蜗杆|螺杆加工「新锦泰」 | 磁力抛光机_磁力研磨机_磁力去毛刺机-冠古设备厂家|维修|租赁【官网】 | 执业药师报名条件,考试时间,考试真题,报名入口—首页 |