您的位置:首页 > 移动开发 > IOS开发

ios Label TextFile 文本来回滚动 包括好用的三方

2017-07-05 16:41 323 查看
通常显示不够了,比如八个字。只能显示6个字。产品要求来回滚动

下面有两种方法:一种UIScrollView一种View动画如果不能满足你请点击这个https://github.com/cbpowell/MarqueeLabel好用的第三方

-(void)viewDidLoad
{
[superviewDidLoad];
self.view.backgroundColor=[UIColorblackColor];

//注册字体
REGISTER_FONT(bundleFont(@"新蒂小丸子体.ttf"),@"新蒂小丸子体");

//获取文本
NSString*string=@"喜欢这首情思幽幽的曲子,仿佛多么遥远,在感叹着前世的情缘,又是那么柔软,在祈愿着来世的缠绵。《莲的心事》,你似琉璃一样的晶莹,柔柔地拨动我多情的心弦。我,莲的心事,有谁知?我,莲的矜持,又有谁懂?";

//初始化label
UILabel*label	=[UILabelnew];
label.text		=string;
label.numberOfLines=0;
label.textColor	=[UIColorcyanColor];
label.font		=[UIFontfontWithName:CUSTOM_FONT(@"新蒂小丸子体",0)
size:20.f];

//计算尺寸
CGSizesize		=[labelboundingRectWithSize:CGSizeMake(0,0)];
label.frame		=(CGRect){CGPointZero,size};

//初始化ScrollView
UIScrollView*showView=\
[[UIScrollViewalloc]initWithFrame:CGRectMake(0,90,320,size.height)];
showView.contentSize=size;
showView.showsHorizontalScrollIndicator=NO;
[showViewaddSubview:label];
[self.viewaddSubview:showView];

//形成边缘的遮罩
UIImageView*imageView=\
[[UIImageViewalloc]initWithFrame:CGRectMake(0,90,320,size.height)];
imageView.image=[UIImageimageNamed:@"bg"];
[self.viewaddSubview:imageView];

//动画
[UIViewanimateKeyframesWithDuration:10
delay:7
options:UIViewKeyframeAnimationOptionAllowUserInteraction
animations:^{
//计算移动的距离
CGPointpoint=showView.contentOffset;
point.x=size.width-320.f;
showView.contentOffset=point;
}
completion:^(BOOLfinished){

}];
}

第二种简洁版


#import"Ridehorselight.h"

@interfaceRidehorselight()

@property(nonatomic,strong)NSTimer*timer;//定时器
@property(nonatomic,weak)UIView*viewAnima;//Label的背景
@property(nonatomic,weak)UILabel*customLabel;//Label

@end

@implementationRidehorselight

-(void)viewDidLoad{
[superviewDidLoad];
//Doanyadditionalsetupafterloadingtheview.

[selfaddClildView];
[selfsetTimer];
}

-(void)addClildView{

self.view.backgroundColor=[UIColorwhiteColor];

CGFloatviewX=(self.view.frame.size.width-200)/2;
UIView*viewAnima=[[UIViewalloc]initWithFrame:CGRectMake(viewX,100,200,40)];
viewAnima.backgroundColor=[UIColoryellowColor];
self.viewAnima=viewAnima;
self.viewAnima.clipsToBounds=YES;//剪掉超出view范围部分

CGFloatcustomLabelY=(self.viewAnima.frame.size.height-30)/2;
UILabel*customLabel=[[UILabelalloc]initWithFrame:CGRectMake(self.viewAnima.frame.size.width,customLabelY,200,30)];
customLabel.text=@"跑马灯效果,滚动文本!";
customLabel.textColor=[UIColorredColor];
self.customLabel=customLabel;

[self.viewaddSubview:viewAnima];
[viewAnimaaddSubview:customLabel];
}

-(void)setTimer{

self.timer=[NSTimerscheduledTimerWithTimeInterval:0.1target:selfselector:@selector(changePos)userInfo:nilrepeats:YES];
}

-(void)changePos{

CGPointcenPos=self.customLabel.center;
if(cenPos.x<-100){

CGFloatdistance=self.customLabel.frame.size.width/2;
self.customLabel.center=CGPointMake(self.viewAnima.frame.size.width+distance,20);
}else{
self.customLabel.center=CGPointMake(cenPos.x-5,20);
}
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: