本文實例為大家分享了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 見代碼
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。