//*********跑马灯代码
//********************ViewController.m中
#import "ViewController.h"
#import "Lightview.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
NSString*text=@"两块钱,你买不了吃亏,两块钱,你买不了上当,真正的物有所值,拿啥啥便宜,买啥啥不贵,都两块,买啥都两块,全场卖两块,随便挑,随便选,都两块!";
Lightview*lv=[[Lightview alloc]initWithFrame:CGRectMake(10, 100, self.view.bounds.size.width-20, 100)title:text];
[self.view addSubview:lv];
[super viewDidLoad];
//***********************Lightview.h中
#import <UIKit/UIKit.h>
@interface Lightview : UIView
-(instancetype)initWithFrame:(CGRect)frame title:(NSString*)tilie;
@end
//*********************Lightview.m中
#import "Lightview.h"
@implementation Lightview
{
CGRect rectMark1;//标记第一个位置
CGRect rectMark2;//标记第二个位置
NSMutableArray*Array;
BOOL isStop;//停止
NSTimeInterval time;
}
-(instancetype)initWithFrame:(CGRect)frame title:(NSString *)tilie
{
self=[super initWithFrame:frame];
if (self) {
tilie=[NSString stringWithFormat:@"%@",tilie];
//时间
time=[self displayDurationForString:tilie];
//显示框颜色
self.backgroundColor=[UIColor blueColor];
//隐藏超出部分(默认为NO)
self.clipsToBounds=YES;
// 字的颜色和大小
UILabel*textLb=[[UILabel alloc]initWithFrame:CGRectZero];
textLb.textColor=[UIColor grayColor];
textLb.font=[UIFont boldSystemFontOfSize:20];
textLb.text=tilie;
// 计算textLb大小
// sizeThatFits返回“最佳”大小适合给定的大小。实际上并没有调整的观点。默认返回现有视图大小
CGSize sizeOfText = [textLb sizeThatFits:CGSizeZero];
//sizeThatFits自适应
// CGSizeZero表示一个巨形框高为零,宽为零,原点位置也为零,需要创建边框但还不确定边框大小和位置时,可以使用此常量
//显示字的位置
rectMark1=CGRectMake(0, 0, sizeOfText.width, self.bounds.size.height);
//滚动的位置((size)宽和高。origin点是x和y)
rectMark2=CGRectMake(rectMark1.origin.x+rectMark1.size.width, 0, sizeOfText.width, self.bounds.size.height);
textLb.frame=rectMark1;
[self addSubview:textLb];
//arrayWithObject也是个方法(滚动前把这些字写在数组里)
Array=[NSMutableArray arrayWithObject:textLb];
// 判断是否需要reserveTextLb
// 如果字的长度>视图长才滚动
BOOL useReserve = sizeOfText.width > frame.size.width ? YES : NO;
//if(YES)== if (useReserve)
if (useReserve) {
// 滚动后字的位置,大小和颜色和原来的字一样
UILabel*reserveTexetLb=[[UILabel alloc]initWithFrame:rectMark2];
reserveTexetLb.textColor=[UIColor redColor];
reserveTexetLb.font=[UIFont boldSystemFontOfSize:20];
reserveTexetLb.text=tilie;
[self addSubview:reserveTexetLb];
//滚动后的字放在数组里
[Array addObject:reserveTexetLb];
//调用下面这个方法
[self paomaAnimate ];
}
}
return self;
}
-(void)paomaAnimate
{
if (!isStop) {
UILabel*lbindex0=Array[0];
UILabel*lbindex1=Array[1];
//控制动画
[UIView transitionWithView:self duration:time options:UIViewAnimationOptionCurveLinear animations:^{
lbindex0.frame=CGRectMake(-rectMark1.size.width, 0, rectMark1.size.width, rectMark1.size.height);
lbindex1.frame=CGRectMake(lbindex0.frame.origin.x+lbindex0.frame.size.width, 0, lbindex1.frame.size.width, lbindex1.frame.size.height);
} completion:^(BOOL finished)
{
lbindex0.frame=rectMark2;
lbindex1.frame=rectMark1;
[Array replaceObjectAtIndex:0 withObject:lbindex1];
[Array replaceObjectAtIndex:1 withObject:lbindex0];
[self paomaAnimate];
}];
}
}
//字幕滚动的速度
-(NSTimeInterval)displayDurationForString:(NSString*)string
{
NSLog(@"%lu",(unsigned long)string.length);
return string.length/30;
}