起因是公司做了小程序,希望app也上,這個(gè)動(dòng)畫開始以為的很難,可是后面看一下沒有想象中那么復(fù)雜,我們來(lái)看一下效果圖
然后貼代碼
@implementation BLEvaluationResultVC
- (void)viewDidLoad {
[super viewDidLoad];
self.fd_prefersNavigationBarHidden = YES;
self.fd_interactivePopDisabled = YES; //設(shè)置不允許滑動(dòng)返回
self.view.backgroundColor = [UIColor whiteColor];
[self setUpView];
[self countJumpAction];
}
- (void)setUpView{
[self.view addSubview:self.scrollView];
[self.view addSubview:self.bottomView];
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.offset(-100);
make.left.right.equalTo(self.view);
make.height.offset(scrollViewH);
}];
[self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.scrollView.mas_bottom);
make.left.right.bottom.equalTo(self.view);
}];
}
- (UIScrollView *)scrollView{
if (!_scrollView) {
NSMutableArray *array = [NSMutableArray array];
[array addObject:@"開始篩選案例"];
[array addObject:@"相似案例匹配"];
[array addObject:@"能力模型分析"];
[array addObject:@"報(bào)告生成中"];
[array addObject:@"方案生成完畢"];
self.titleArr = array;
_scrollView = [[UIScrollView alloc]init];
_scrollView.backgroundColor = [UIColor whiteColor];
_scrollView.delegate = self;
_scrollView.contentInset = UIEdgeInsetsMake(scrollViewH *0.5 , 0, 0, 0);
_scrollView.contentSize = CGSizeMake(0, scrollViewH * 0.5 - labelH * 0.5 );
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.userInteractionEnabled = NO;
[self.titleArr enumerateObjectsUsingBlock:^(NSString* _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
UILabel *titleLab = [[UILabel alloc] init];
titleLab.text = title;
titleLab.textAlignment = NSTextAlignmentCenter;
titleLab.frame = CGRectMake(0, idx*labelH, kScreenWidth, labelH);
titleLab.tag = idx;
[_scrollView addSubview:titleLab];
if (idx==0) {
self.currentTag = idx;
self.currentLab = titleLab;
titleLab.textColor = RGBColor(76, 76, 77);
titleLab.font = [UIFont boldSystemFontOfSize:20];
}else{
titleLab.textColor = kBLBlack;
titleLab.font = [UIFont systemFontOfSize:14];
}
}];
self.ms = 0;
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTriggered:)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
return _scrollView;
}
- (void)completeAnimation{
[self.displayLink invalidate];
self.displayLink = nil;
for (UIView *subview in self.scrollView.subviews) {
[subview removeFromSuperview];
}
UILabel *titleLab = [[UILabel alloc] init];
titleLab.text = @"方案生成完畢";
titleLab.font = [UIFont systemFontOfSize:20];
titleLab.textColor = kBLBlack;
titleLab.textAlignment = NSTextAlignmentCenter;
titleLab.frame = CGRectMake(0, (self.titleArr.count - 1) * labelH, kScreenWidth, labelH);
[self.scrollView addSubview:titleLab];
}
- (void)displayLinkTriggered:(CADisplayLink *)link{
self.scrollView.contentOffset = CGPointMake(0, self.scrollView.contentOffset.y+1);
self.ms += 1;
if (self.ms == (self.titleArr.count - 1) * 60 + 30) {
[self completeAnimation];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
if (self.ms < 30) {
self.currentLab.font = [UIFont systemFontOfSize:20-self.ms/30.f*(20-14)];
} else {
int currentMS = self.ms-30;
self.currentTag = (currentMS)/labelH+1;
UILabel *lab = (UILabel *)[self.scrollView viewWithTag:self.currentTag];
//處理文字大小
int count = currentMS%60/30;//在label的中間的上面還是下面
//下面 文字開始從20變到14
if (count) {
lab.font = [UIFont systemFontOfSize:20-currentMS%30/30.f*(20-14)];
} else {
//上面 文字開始從14變到20
lab.font = [UIFont systemFontOfSize:14+currentMS%30/30.f*(20-14)];
}
//處理文字顏色
if (self.currentLab!=lab) {
self.currentLab.textColor = RGBColor(76, 76, 77);
lab.textColor = kBLBlack;
self.currentLab = lab;
}
}
});
}
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self completeAnimation];
}
//導(dǎo)航欄左上角返回按鈕
- (void)backToUpView
{
[self.navigationController popViewControllerAnimated:YES];
}
- (UIImageView *)bottomView{
if (!_bottomView) {
_bottomView = [[UIImageView alloc]init];
_bottomView.image = [UIImage imageNamed:@"resultbg"];
_bottomView.userInteractionEnabled = YES;
self.countLab = [[UILabel alloc]init];
self.countLab.text = @"20000+";
self.countLab.font = [UIFont boldSystemFontOfSize:34];
self.countLab.textColor = [UIColor whiteColor];
self.countLab.textAlignment = NSTextAlignmentCenter;
// 獲取代表公歷的NSCalendar對(duì)象
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// 獲取當(dāng)前日期
NSDate* dt = [NSDate date];
// 定義一個(gè)時(shí)間字段的旗標(biāo),指定將會(huì)獲取指定年符糊、月、日榛斯、時(shí)、分宇立、秒的信息
unsigned unitFlags = NSCalendarUnitYear |
NSCalendarUnitMonth | NSCalendarUnitDay |
NSCalendarUnitHour | NSCalendarUnitMinute |
NSCalendarUnitSecond | NSCalendarUnitWeekday;
// 獲取不同時(shí)間字段的信息
NSDateComponents* comp = [gregorian components: unitFlags fromDate:dt];
// 獲取各時(shí)間字段的數(shù)值
BLLog(@"現(xiàn)在是%ld年" , comp.year);
self.targetLab = [[UILabel alloc]init];
self.targetLab.text =[NSString stringWithFormat:@"%ld年%@%@競(jìng)爭(zhēng)對(duì)手預(yù)測(cè)",comp.year,self.country,self.profession];
self.targetLab.font = [UIFont boldSystemFontOfSize:16];
self.targetLab.textColor = [UIColor whiteColor];
self.targetLab.textAlignment = NSTextAlignmentCenter;
self.checkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.checkBtn.backgroundColor = [UIColor whiteColor];
self.checkBtn.layer.cornerRadius = 5.0;
[self.checkBtn setTitle:@"查看我的能力模型" forState:UIControlStateNormal];
[self.checkBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.checkBtn addTarget:self action:@selector(checkButtonClick) forControlEvents:UIControlEventTouchUpInside];
[_bottomView addSubview:self.countLab];
[_bottomView addSubview:self.targetLab];
[_bottomView addSubview:self.checkBtn];
[self.countLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(_bottomView.mas_centerY).offset(-10);
make.left.right.equalTo(_bottomView);
}];
[self.targetLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.countLab.mas_bottom).offset(20);
make.left.right.equalTo(_bottomView);
}];
[self.checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(_bottomView.mas_bottom).offset(-30);
make.left.equalTo(_bottomView.mas_left).mas_offset(40);
make.right.equalTo(_bottomView.mas_right).mas_offset(-40);
make.height.offset(40);
}];
}
return _bottomView;
}
- (void)checkButtonClick{
BLEvaluationReportVC *ReportVC = [[BLEvaluationReportVC alloc]init];
ReportVC.type = self.type;
[self.navigationController pushViewController:ReportVC animated:YES];
}
///文字跳動(dòng)動(dòng)畫
- (void)countJumpAction
{
__block int _numText = 0;
int _endText = [self.Jzl intValue]; //最后總數(shù)據(jù)
int _step = 80; //累加數(shù)量
double _stepSecond = (self.titleArr.count - 1) * 1.0 / (_endText / _step);// 5.0動(dòng)畫總時(shí)間
//全局隊(duì)列 默認(rèn)優(yōu)先級(jí)
dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, quene);
//NSEC_PER_SEC是秒慨飘,*1是每秒
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), NSEC_PER_SEC * _stepSecond, 0);
dispatch_source_set_event_handler(timer, ^{
//回調(diào)主線程,在主線程中操作UI
dispatch_async(dispatch_get_main_queue(), ^{
if (_numText <= _endText) {
self.countLab.text = [NSString stringWithFormat:@"%.d",_numText];
_numText+=_step;
}else{
dispatch_source_cancel(timer);
self.countLab.text = [NSString stringWithFormat:@"%d+",_endText];
}
});
});
dispatch_resume(timer);
}
- (void)dealloc{
BLLog(@"銷毀");
}