使用UIScrollView和CADisplayLink实现文字滚动到某个区域放大动画

起因是公司做了小程序,希望app也上,这个动画开始以为的很难,可是后面看一下没有想象中那么复杂,我们来看一下效果图


使用UIScrollView和CADisplayLink实现文字滚动到某个区域放大动画_第1张图片
测评结果动画.gif

然后贴代码

@implementation BLEvaluationResultVC

- (void)viewDidLoad {
[super viewDidLoad];


self.fd_prefersNavigationBarHidden = YES;
self.fd_interactivePopDisabled = YES; //设置不允许滑动返回
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:@"报告生成中"];
    [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];
}


//导航栏左上角返回按钮
- (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对象
    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate* dt = [NSDate date];
    // 定义一个时间字段的旗标,指定将会获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    NSDateComponents* comp = [gregorian components: unitFlags fromDate:dt];
    // 获取各时间字段的数值
    BLLog(@"现在是%ld年" , comp.year);

    self.targetLab = [[UILabel alloc]init];
    self.targetLab.text =[NSString stringWithFormat:@"%ld年%@%@竞争对手预测",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];


}

///文字跳动动画
- (void)countJumpAction
{
__block int _numText = 0;
int _endText = [self.Jzl intValue]; //最后总数据
int _step = 80; //累加数量
double _stepSecond =  (self.titleArr.count - 1) * 1.0 / (_endText / _step);// 5.0动画总时间
//全局队列    默认优先级
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, ^{
    //回调主线程,在主线程中操作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(@"销毁");
}

你可能感兴趣的:(使用UIScrollView和CADisplayLink实现文字滚动到某个区域放大动画)