iOS开发之跑马灯

2017-01-14 10:45:36来源:http://www.jianshu.com/p/de375970bd46作者:带我逃跑吧人点击

第七城市

1.法一:
类似于弹幕,一个label的实现


- (void)viewDidLoad {
[super viewDidLoad];
[self addPaomaLabel];

}


- (void)addPaomaLabel {
self.annonLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 8, ScreenWidth-45, 30)];
self.annonLabel.text = @"哈哈哈哈带我逃跑吧的简书哈哈哈哈哈今年的目标要减肥哈哈哈";
self.annonLabel.font = [UIFont systemFontOfSize:14.0f];
self.annonLabel.textColor = [UIColor grayColor];
self.annonLabel.numberOfLines = 1;
[self.annonLabel sizeToFit];
[self.backView addSubview:self.annonLabel];
[self.backView sendSubviewToBack:self.annonLabel];

}


  - (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.annonLabel.center = CGPointMake(self.view.bounds.size.width +self.annonLabel.bounds.size.width/2, self.annonLabel.center.y);
[self anmationForNoticeLabel];

}


-(void)anmationForNoticeLabel{
self.annonLabel.center = CGPointMake(self.view.bounds.size.width + self.annonLabel.bounds.size.width/2, self.annonLabel.center.y);
[UIView animateWithDuration:10 animations:^{
self.annonLabel.center = CGPointMake(self.annonLabel.center.x - self.annonLabel.bounds.size.width-self.view.bounds.size.width, self.annonLabel.center.y);
} completion:^(BOOL finished) {
if (finished) {
if (_isAnimation) {
[self anmationForNoticeLabel];
}
}
}];

}


2.法二:两个label组成的跑马灯,前面文字消失,后面文字出现


- (void)viewDidLoad {
[super viewDidLoad];
[self addTwoLabelPaoMa];
}
- (void)addTwoLabelPaoMa {
self.annonLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 8, ScreenWidth-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.backView addSubview:self.annonLabel];
[self.backView addSubview:self.secondLabel];
[self.backView sendSubviewToBack:self.annonLabel];
[self.backView 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];
}];
}


法二实现效果.gif


法二demo地址:
http://download.csdn.net/download/simona_1973/9670022




第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台