学习iOS全局跑马灯
程序员文章站
2023-12-18 15:00:16
本文实例为大家分享了ios全局跑马灯制作方法,供大家参考,具体内容如下
思路:
1.创建一个单例
+ (instancetype)sharemanager {...
本文实例为大家分享了ios全局跑马灯制作方法,供大家参考,具体内容如下
思路:
1.创建一个单例
+ (instancetype)sharemanager { static ccpaomaview *pmodel = nil; static dispatch_once_t once; dispatch_once(&once, ^{ pmodel = [[ccpaomaview alloc]initwithframe:cgrectmake(0, 0, kscreenwidth, 0.0468 *kscreenheight)]; }); return pmodel; }
2.把接收的数据存在本地的 plist,不适用于大量数据,每次读取第一个数据,读取完成删除整个 plist,再把剩下的重新存进 plist
3.根据动画代理,监听动画执行结束,将动画实例置为 nil
- (void)animationdidstop:(caanimation *)anim finished:(bool)flag{ nslog(@"%@",[self.paomalabel.layer animationforkey:@"paomadeng"]); if ([self.paomalabel.layer animationforkey:@"paomadeng"] == anim) { //移除第一组数据 [_array removeobjectatindex:0]; //移除整个 plist [ccpaomamodel removepaomaplist]; //重新写入 [_array writetofile:[ccpaomamodel filename] atomically:yes]; //动画停止之后,将实例置为 nil _pmaniamtion = nil; //数组为空之后移除跑马灯 if (_array.count > 0) { [self showpaomaview:self.superview]; }else{ self.hidden = yes; [self removefromsuperview]; } nslog(@"%@",self.array); } }
4.判断动画实例是否为空,使得切换界面,跑马灯继续,而不是重新开始
- (void)paomaaniamtion:(cgfloat)count{ //判断动画实例存不存在,存在继续,不存在即创建 if (_pmaniamtion == nil) { _pmaniamtion = [cabasicanimation animation]; _pmaniamtion.keypath = @"transform.translation.x"; cgfloat w = cgrectgetwidth(_paomalabel.bounds); _pmaniamtion.fromvalue = @(w); _pmaniamtion.tovalue = @(-w); _pmaniamtion.duration = _anitime; _pmaniamtion.repeatcount = count; _pmaniamtion.removedoncompletion = no; //动画结束不移除 _pmaniamtion.fillmode = kcafillmodeforwards; //动画结束会保持结束的状态 _pmaniamtion.delegate = self; //设置代理 [_paomalabel.layer addanimation:_pmaniamtion forkey:@"paomadeng"]; }else{ _pmaniamtion.repeatcount = count; } }
5.切换界面,暂停、恢复动画
#pragma mark -- 界面出现、消失,创建、恢复、暂停动画 - (void)viewwillappear:(bool)animated { [_paomaview showpaomaview:self.view]; [_paomaview resumeanimation]; } - (void)viewwilldisappear:(bool)animated { [_paomaview pauseaniamtion]; }
话不多说,github 见代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。