使用UIScrollView和CADisplayLink實(shí)現(xiàn)文字滾動(dòng)到某個(gè)區(qū)域放大動(dòng)畫

起因是公司做了小程序,希望app也上,這個(gè)動(dòng)畫開始以為的很難,可是后面看一下沒有想象中那么復(fù)雜,我們來(lái)看一下效果圖


測(cè)評(píng)結(jié)果動(dòng)畫.gif

然后貼代碼

@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(@"銷毀");
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末国撵,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子玻墅,更是在濱河造成了極大的恐慌介牙,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件澳厢,死亡現(xiàn)場(chǎng)離奇詭異环础,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)剩拢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門线得,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人徐伐,你說我怎么就攤上這事贯钩。” “怎么了办素?”我有些...
    開封第一講書人閱讀 162,577評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵角雷,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我性穿,道長(zhǎng)勺三,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,176評(píng)論 1 292
  • 正文 為了忘掉前任需曾,我火速辦了婚禮吗坚,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘呆万。我一直安慰自己刻蚯,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評(píng)論 6 388
  • 文/花漫 我一把揭開白布桑嘶。 她就那樣靜靜地躺著,像睡著了一般躬充。 火紅的嫁衣襯著肌膚如雪逃顶。 梳的紋絲不亂的頭發(fā)上讨便,一...
    開封第一講書人閱讀 51,155評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音以政,去河邊找鬼霸褒。 笑死,一個(gè)胖子當(dāng)著我的面吹牛盈蛮,可吹牛的內(nèi)容都是我干的废菱。 我是一名探鬼主播,決...
    沈念sama閱讀 40,041評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼抖誉,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼殊轴!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起袒炉,我...
    開封第一講書人閱讀 38,903評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤旁理,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后我磁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體孽文,經(jīng)...
    沈念sama閱讀 45,319評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評(píng)論 2 332
  • 正文 我和宋清朗相戀三年夺艰,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了芋哭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,703評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡郁副,死狀恐怖减牺,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情霞势,我是刑警寧澤烹植,帶...
    沈念sama閱讀 35,417評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站愕贡,受9級(jí)特大地震影響草雕,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜固以,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評(píng)論 3 325
  • 文/蒙蒙 一墩虹、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧憨琳,春花似錦诫钓、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至遍略,卻和暖如春惧所,著一層夾襖步出監(jiān)牢的瞬間骤坐,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工下愈, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留纽绍,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,711評(píng)論 2 368
  • 正文 我出身青樓势似,卻偏偏與公主長(zhǎng)得像拌夏,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子履因,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,072評(píng)論 25 707
  • 1障簿、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫(kù)組件 SD...
    陽(yáng)明先生_X自主閱讀 15,979評(píng)論 3 119
  • 很想去為自己努力一把,不想浪漫了時(shí)光搓逾,畢竟人的一生很短卷谈,過了這個(gè)年華,再也沒機(jī)會(huì)了霞篡。不是嗎世蔗?
    一只檸檬味的貓閱讀 197評(píng)論 0 0
  • 每個(gè)人都不一樣,有效率的人每天可以做很多事朗兵,而效率慢的呢總是一天拖過一天污淋。 但人生也就這幾十年時(shí)光,浪費(fèi)的都是自己...
    純妹子閱讀 287評(píng)論 0 0
  • 在那美麗的清漳河畔 有我心中最美的故鄉(xiāng) 所有童年的歡樂時(shí)光 總有清漳河深情擁抱 長(zhǎng)大后常常身不由己 為追夢(mèng)不惜遠(yuǎn)走...
    路雨飛飛閱讀 368評(píng)論 0 2