ios 滚动label 快速实现源码。这里使用的两个label,挪动frame实现。简单使用,这里仅抛砖引玉而已

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *str = @"I love you,IOS-文字滚动的Label!";

self.scrollLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,100,self.view.frame.size.width, 44)];
self.scrollLabel.text = str;
[self.view addSubview:self.scrollLabel];


self.secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.scrollLabel.frame.origin.x+self.scrollLabel.frame.size.width, self.scrollLabel.frame.origin.y, self.scrollLabel.frame.size.width, self.scrollLabel.frame.size.height)];
self.secondLabel.font = self.scrollLabel.font;
self.secondLabel.text = self.scrollLabel.text;
[self.view addSubview:self.secondLabel];

[self addAnimation];
// [[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(addAnimation) userInfo:nil repeats:YES] fire];
}

- (void)addAnimation{
CGRect scrollFrame = self.scrollLabel.frame;
CGRect secondFrame = self.secondLabel.frame;

[UIView animateWithDuration:4 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
self.scrollLabel.frame = CGRectMake(-self.scrollLabel.frame.size.width, self.scrollLabel.frame.origin.y, self.scrollLabel.frame.size.width, self.scrollLabel.frame.size.height);
self.secondLabel.frame = CGRectMake(0, self.secondLabel.frame.origin.y, self.secondLabel.frame.size.width, self.secondLabel.frame.size.height);
} completion:^(BOOL finished) {
self.scrollLabel.frame = scrollFrame;
self.secondLabel.frame = secondFrame;
[self addAnimation];
}];
}