封装了一个iOS媒体标题走马灯轮播视图

效果图

请添加图片描述

思路

底层放置一个scrollview,上面
放置两个内容相同的label, 滚动的到第二个的时候,立即将scrollview的偏移量置为第一个,停留一段时间之后开始下一轮滚动

核心功能代码

- (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;
}


使用方法

    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];

猜你喜欢

转载自blog.csdn.net/LIUXIAOXIAOBO/article/details/131873910