参考:http://www.xuebuyuan.com/2085033.html
核心代码:
- (void)awakeFromNib {
self.separatorInset=UIEdgeInsetsMake(0,400, 0, 0);
self.WellComeLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 0, 10, 45)];
self.WellComeLabel.font=[UIFont systemFontOfSize:15];
self.WellComeLabel.text=@"ljslglhiwonlahglshggethehretaglhggggirsrhjeiltahnilenthlijjhdljhltjlhnilenh";
self.WellComeLabel.numberOfLines=1;
//自适应文字宽度
CGSize Newsize=[self.WellComeLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 45) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.WellComeLabel.font} context:nil].size;
self.WellComeLabel.frame=CGRectMake(20, 0, Newsize.width, 45);
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(30, 0, screen_width-30, 45)];
[self.contentView addSubview:scrollView];
[scrollView addSubview:self.WellComeLabel];
if (Newsize.width>screen_width-50) {
[self animationLabel];
}
}
-(void)animationLabel{
int i=self.WellComeLabel.frame.size.width-(screen_width-50)+20;
[self.WellComeLabel.layer removeAllAnimations];
[UIView animateWithDuration:3.0
delay:0
options:UIViewAnimationOptionRepeat //动画重复的主开关
|UIViewAnimationOptionAutoreverse //动画重复自动反向,需要和上面这个一起用
|UIViewAnimationOptionCurveLinear //动画的时间曲线,滚动字幕线性比较合理
animations:^{
self.WellComeLabel.frame=CGRectMake(self.WellComeLabel.frame.origin.x-i, 0, self.WellComeLabel.frame.size.width, self.WellComeLabel.frame.size.height);
}
completion:^(BOOL finished) {
self.WellComeLabel.frame=CGRectMake(20, 0, self.WellComeLabel.frame.size.width, self.WellComeLabel.frame.size.height);
[self animationLabel];//加上这句 就不会别其他动画打断
}
];
}
常规动画属性设置(可以同时选择多个进行设置)
UIViewAnimationOptionLay
UIViewAnimationOptionAll
UIViewAnimationOptionBeg
UIViewAnimationOptionRep
UIViewAnimationOptionAut
UIViewAnimationOptionOve
UIViewAnimationOptionOve
UIViewAnimationOptionAll
UIViewAnimationOptionSho
UIViewAnimationOptionOve
2.动画速度控制(可从其中选择一个设置)
UIViewAnimationOptionCur
UIViewAnimationOptionCur
UIViewAnimationOptionCur
UIViewAnimationOptionCur
3.转场类型(仅适用于转场动画设置,可以从中选择一个进行设置,基本动画、关键帧动画不需要设置)
UIViewAnimationOptionTra
UIViewAnimationOptionTra
UIViewAnimationOptionTra
UIViewAnimationOptionTra
UIViewAnimationOptionTra
UIViewAnimationOptionTra
UIViewAnimationOptionTra
UIViewAnimationOptionTra