.m
@property UILabel *annonLabel;
@property UILabel *secondLabel;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self addTwoLabelPaoMa];
}
- (void)addTwoLabelPaoMa {
self.annonLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 228, self.annonLabel.frame.size.width-45, 30)];
self.annonLabel.text = @"今天看着六六感觉又胖了,昨天做的内外驱虫,早上大便也没看到虫子,卫生情况良好哈哈哈哈哈";
self.annonLabel.font = [UIFont systemFontOfSize:12.0f];
self.annonLabel.numberOfLines = 1;
[self.annonLabel sizeToFit];
self.secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.annonLabel.frame.origin.x+self.annonLabel.frame.size.width, self.annonLabel.frame.origin.y, self.annonLabel.frame.size.width, self.annonLabel.frame.size.height)];
self.secondLabel.text = self.annonLabel.text;
self.secondLabel.font = self.annonLabel.font;
self.secondLabel.numberOfLines = 1;
[self.secondLabel sizeToFit];
[self.view addSubview:self.annonLabel];
[self.view addSubview:self.secondLabel];
[self.view sendSubviewToBack:self.annonLabel];
[self.view sendSubviewToBack:self.secondLabel];
[self addAnimation];
}
- (void)addAnimation{
CGRect scrollFrame = self.annonLabel.frame;
CGRect secondFrame = self.secondLabel.frame;
// 10为动画时间,我这里model文字比较长,你们可自行设置短一点
[UIView animateWithDuration:10 delay:0 options:UIViewAnimationOptionCurveLinear
animations:^{
self.annonLabel.frame = CGRectMake(-self.annonLabel.frame.size.width, self.annonLabel.frame.origin.y, self.annonLabel.frame.size.width, self.annonLabel.frame.size.height);
// 40是文字较多,所以在消失后出现时设置间距为40,文字较少可直接设置为0
self.secondLabel.frame = CGRectMake(40, self.secondLabel.frame.origin.y, self.secondLabel.frame.size.width, self.secondLabel.frame.size.height);
} completion:^(BOOL finished) {
self.annonLabel.frame = scrollFrame;
self.secondLabel.frame = secondFrame;
[self addAnimation];
}];
}
这是一个比较简单的跑马灯效果还有另一种,这个代码比较多,直接上链接:
csdn:http://download.csdn.net/download/wangqinglei0307/10195483
github:https://github.com/WangQingLei307909/PaoMaDeng_wql.git