今天做了一个字体渐变出现的小动画,代码如下:
在controller中:
- (void) test{
_i=0;
self.view.backgroundColor= [UIColorwhiteColor];
self.testStr=@"生活需要你坦然的面对,坚强的撑起,因为下一个路口也许就是奇迹!";
UIView*m_mengbanView = [[UIViewalloc]init];
[self.viewaddSubview:m_mengbanView];
[m_mengbanViewmas_makeConstraints:^(MASConstraintMaker*make) {
make.left.mas_equalTo(self.view).mas_offset(limit5sW(13));
make.top.mas_equalTo(self.view).mas_offset(kScreenHeight-limit5sH(394/2.0) -limit5sH(13));
make.width.mas_equalTo(kScreenWidth-limit5sW(13) *2);
make.height.mas_equalTo(limit5sH(394/2.0));
}];
_textArray= [NSMutableArrayarray];
for(intj =1; j<=self.testStr.length; j++) {
NSRangerange =NSMakeRange(0, j);
NSString*str = [self.testStrsubstringWithRange:range];
[_textArrayaddObject:str];
}
CGFloatnocompare =0;
_dateDayLabel= [[UILabelalloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) /2.0-limit5sW(55) -limit5sW(8),limit5sH(15),limit5sW(100),limit5sH(70) + nocompare)];
_dateDayLabel.adjustsFontSizeToFitWidth=YES;
_dateDayLabel.textAlignment=NSTextAlignmentCenter;
_dateDayLabel.textColor= [UIColorwhiteColor];
[m_mengbanViewaddSubview:_dateDayLabel];
_dateMoonLabel= [[UILabelalloc]initWithFrame:CGRectMake(CGRectGetMaxX(_dateDayLabel.frame) -limit5sW(13),CGRectGetMaxY(_dateDayLabel.frame) -limit5sH(20),limit5sW(120),limit5sH(20))];
_dateMoonLabel.textColor= [UIColorwhiteColor];
[m_mengbanViewaddSubview:_dateMoonLabel];
_imageTitleLabel= [[UILabelalloc]initWithFrame:CGRectMake(limit5sW(30),CGRectGetMaxY(_dateDayLabel.frame) +limit5sH(16),kScreenWidth-limitW(80),limitH(40))];
_imageTitleLabel.numberOfLines=0;
_imageTitleLabel.textColor= [UIColorredColor];
_imageTitleLabel.lineBreakMode=NSLineBreakByWordWrapping;
_imageTitleLabel.adjustsFontSizeToFitWidth=YES;
[m_mengbanViewaddSubview:_imageTitleLabel];
CGRectcontentL =_imageTitleLabel.frame;
if(self.testStr.length>19){
contentL.size.height+=10;
contentL.origin.y-=1;
}else{
contentL.origin.y-=6;
}
_imageTitleLabel.frame= contentL;
if(iPhone6){
CGRectcontentL =_imageTitleLabel.frame;
if(self.testStr.length>17){
contentL.size.height+=10;
contentL.origin.y-=1;
}else{
contentL.origin.y-=6;
}
_imageTitleLabel.frame= contentL;
CGRectrect1 =_dateMoonLabel.frame;
rect1.origin.x-=14;
_dateMoonLabel.frame= rect1;
CGRectrect2 =_dateDayLabel.frame;
rect2.origin.x-=24;
_dateDayLabel.frame= rect2;
}elseif(iPhone6P){
CGRectcontentL =_imageTitleLabel.frame;
if(self.testStr.length>17){
contentL.size.height+=10;
contentL.origin.y-=11;
}else{
contentL.origin.y-=7;
}
_imageTitleLabel.frame= contentL;
CGRectdateR =_dateDayLabel.frame;
dateR.origin.y-=30;
dateR.origin.x-=9;
dateR.size.height+=27;
_dateDayLabel.frame= dateR;
CGRectmoonR =_dateMoonLabel.frame;
moonR.origin.y-=2;
_dateMoonLabel.frame= moonR;
CGRectdataR =_dateDayLabel.frame;
dataR.origin.y+=limit5sH(14);
_dateDayLabel.frame= dataR;
}
_timer= [NSTimerscheduledTimerWithTimeInterval:0.2target:selfselector:@selector(print)userInfo:nilrepeats:YES];
}
- (void)print{
if(_i<_textarray.count>
_imageTitleLabel.text=_textArray[_i];
}
if(_i==_textArray.count) {
[_timerinvalidate];
_timer=nil;
}
_i+=1;
}
至此就可以实现字体渐变出现的小动画了,快来操作实现一下吧~