renderings
train of thought
Place a scrollview at the bottom, and
place two labels with the same content on it. When scrolling to the second one, immediately set the offset of the scrollview to the first one, and start the next round of scrolling after staying for a while
core function code
- (void)setUpUI
{
[self addSubview:self.scrollView];
CGFloat origin = 0;
for (int i = 0; i < 2; i ++) {
NSString *string = self.configuration.text;
CGFloat width = [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, self.bounds.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.configuration.font} context:nil].size.width;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(origin + i * self.configuration.itemSpace, 0, width, self.bounds.size.height)];
label.font = self.configuration.font;
label.textColor = self.configuration.textColor;
label.text = self.configuration.text;
origin = CGRectGetMaxX(label.frame);
[self.scrollView addSubview:label];
if (i == 1) {
self.secondOriginx = CGRectGetMinX(label.frame);
self.scrollView.contentSize = CGSizeMake(CGRectGetMaxX(label.frame), 0);
}
}
}
- (void)linkStart
{
self.isLink = YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.configuration.delayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateContentOffset)];
[link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
self.link = link;
});
}
- (void)animationStart
{
NSTimeInterval duration = self.secondOriginx / self.configuration.rate;
[UIView animateWithDuration:duration delay:self.configuration.delayTime options:UIViewAnimationOptionCurveLinear animations:^{
[self.scrollView setContentOffset:CGPointMake(self.secondOriginx, 0) animated:NO];
} completion:^(BOOL finished) {
[self.scrollView setContentOffset:CGPointZero];
[self animationStart];
}];
}
- (void)updateContentOffset
{
[self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + self.configuration.rate, 0)];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (!self.isLink) {
return;
}
if (scrollView.contentOffset.x >= self.secondOriginx) {
scrollView.contentOffset = CGPointMake(0, 0);
[self.link invalidate];
self.link = nil;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.configuration.delayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self linkStart];
});
}
}
#pragma mark - lazy load
- (UIScrollView *)scrollView
{
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.delegate = self;
}
return _scrollView;
}
Instructions
LBMaqueeLabelConfig *configuration = [[LBMaqueeLabelConfig alloc] init];
configuration.text = @"顺治四年(1647年)六月,南明官军王祥部总";
configuration.textColor = [UIColor cyanColor];
configuration.font = [UIFont systemFontOfSize:19];
configuration.rate = 2;
configuration.delayTime = 2;
LBMarqueeLabel *label = [[LBMarqueeLabel alloc] initWithFrame:CGRectMake(20, 400, CGRectGetWidth(self.view.bounds) - 200, 40) configuration:configuration];
[self.view addSubview:label];
label.backgroundColor = [UIColor redColor];
[label linkStart];