思路:
1.创建一个单例
+ (instancetype)shareManager {
static CCPaomaView *pModel = nil;
static dispatch_once_t once;
dispatch_once(&once, ^{
pModel = [[CCPaomaView alloc]initWithFrame:CGRectMake(, , KScreenWidth, 0.0468 *KScreenHeight)];
});
return pModel;
}
2.使用 FMDB,把接收的数据存在本地的 sqlite
,每次取一个数据,动画执行完删除当前数据,再取下一条数据
3.根据动画代理,监听动画执行结束,将动画实例置为 nil
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
NSLog(@"stopAnimation:%@",[self.paomaLabel.layer animationForKey:@"paoMaDeng"]);
// 记录运行的动画的时间
_pmAniamtion.timeOffset = anim.timeOffset; // 如果回到前台
NSString *backStr = [_defaults objectForKey:@"isBack"];
if ([backStr intValue] == ) {
if ([self.paomaLabel.layer animationForKey:@"paoMaDeng"] == anim) { [_timer invalidate];
_timer = nil; // 动画停止之后,将实例置为 nil
_pmAniamtion = nil; CCPaomaModel *model = [[CCPaomaModel alloc]init];
// 删除第一个数据
[model deleteRow:_array[]];
// 重新再取一次数据(一个)
_array = [model selectTable]; // 数组为空之后移除跑马灯
if (_array.count > ) {
[self showPaomaView:self.superview];
}else{
self.hidden = YES;
[self removeFromSuperview];
}
NSLog(@"%@",self.array);
}
}else{
// 暂停定时器
[_timer setFireDate:[NSDate distantFuture]];
}
}
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.切换界面,暂停、恢复动画
- (void)viewWillAppear:(BOOL)animated { [_paomaView resumeAnimation]; if (_paomaView.hidden == YES) {
[_paomaView showPaomaView:self.view];
} if (_paomaView.array.count > ) {
[self.view addSubview:_paomaView];
}
} - (void)viewWillDisappear:(BOOL)animated {
[_paomaView pauseAnimation]; [_paomaView.timer invalidate];
_paomaView.timer = nil;
}
6.记录回到后台,判断是否执行添加动画方法
- (void)resumeAnimation
{
if (_array.count > ) {
CFTimeInterval pausedTime = [_paomaLabel.layer timeOffset];
_paomaLabel.layer.speed = 1.0;
_paomaLabel.layer.timeOffset = 0.0;
_paomaLabel.layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [_paomaLabel.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
_paomaLabel.layer.beginTime = timeSincePause; // 是否进入后台
_defaults = [NSUserDefaults standardUserDefaults];
NSString *backStr = [_defaults objectForKey:@"isBack"];
if ([backStr intValue] == ) { // 开启定时器
[_timer setFireDate:[NSDate distantPast]]; int anmationCount = [_array[] intValue];
NSLog(@"恢复之后的次数:%d",anmationCount);
if (anmationCount > ) {
_pmAniamtion.repeatCount = anmationCount;
[_paomaLabel.layer addAnimation:_pmAniamtion forKey:@"paoMaDeng"];
}
[_defaults setObject:@"" forKey:@"isBack"];
}
}
}
话不多说,github 见代码 ( https://github.com/slodier/CCScoops ),觉得好给个 star 呗