无限向上滚动公告实现

思路:一般公告都是以文本为主,所以使用UIScrollView+UILabel便可以实现。由于项目需求,滚动公告一行需要展示4个UILabel,第四个UILabel的textColor不一样,且一次展示多行,效果如下图,所以考虑使用UIScrollView+UITableView。

公告滚动图

.h文件

#import 

@interface NoticeScrollView : UIView

@property (strong, nonatomic) UIScrollView *MainScrollView;
@property (nonatomic, strong) UITableView *scrollTableView;

@property (nonatomic, copy) NSMutableArray *dataArr;//将数据传入

@end

.m文件

#import "NoticeScrollView.h"

const int  showRows = 5;//公告一次性展示的行数
const float showRowsHeight = 44; //公告每行的高度

@interface NoticeScrollView ()

/* 定时器  */
@property (nonatomic,strong) NSTimer *timer;

@end

@implementation NoticeScrollView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.MainScrollView];
    }
    return self;
}

- (void)setDataArr:(NSMutableArray *)dataArr {
    
    _dataArr = dataArr;
    self.MainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, showRowsHeight * _dataArr.count);
    self.scrollTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, showRowsHeight * _dataArr.count);
    [self.scrollTableView reloadData];
    [self removeTimer];
    if (_dataArr.count > showRows) {
        [self addTimer];
    }
}

- (void)addTimer {
    /*
     scheduledTimerWithTimeInterval:  滑动视图的时候timer会停止
     这个方法会默认把Timer以NSDefaultRunLoopMode添加到主Runloop上,而当你滑tableView的时候,就不是NSDefaultRunLoopMode了,这样,你的timer就会停了。
     self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextLabel) userInfo:nil repeats:YES];
     */
    
    self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(nextLabel) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)nextLabel {
    
    CGPoint oldPoint = self.MainScrollView.contentOffset;
    oldPoint.y += showRowsHeight;
    [self.MainScrollView setContentOffset:oldPoint animated:YES];
}

#pragma mark 【 UITableViewDataSource 】
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; {
    
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
    }
    [cell.contentView removeAllSubviews];
//需要公告显示什么就往contentView上添加什么即可
    for (int i = 0; i < 4; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(i*SCREEN_WIDTH/4, 0, SCREEN_WIDTH/4, showRowsHeight)];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = DetailTextFont;
        label.textColor = [UIColor darkGrayColor];
        NSArray *arr = [_dataArr objectAtIndex:indexPath.row];
        if (_dataArr.count > 0) {
            label.text = [arr objectAtIndex:i];
            if (i == 4) {
                if ([[arr objectAtIndex:i] isEqualToString:@"已交车"]) {
                    label.textColor = [UIColor redColor];
                }else{
                    label.textColor = MainColor;
                }
            }
        }
        [cell.contentView addSubview:label];
    }
    return cell;
}
#pragma mark  【 UITableViewDelegate 】
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; {
    
    return showRowsHeight;
}

#pragma mark 【 UIScrollViewDelegate 】
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSLog(@"%f",self.MainScrollView.contentOffset.y);
    if (self.MainScrollView.contentOffset.y == (_dataArr.count * showRowsHeight - showRowsHeight * showRows)) {
        [self.MainScrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    }
}

#pragma mark 【 Lazy Load 】
- (UIScrollView *)MainScrollView {
    
    if (!_MainScrollView) {
        _MainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, showRowsHeight * showRows)];
        _MainScrollView.delegate = self;
        _MainScrollView.scrollEnabled = NO;
        [_MainScrollView addSubview:self.scrollTableView];
    }
    return _MainScrollView;
}

- (UITableView *)scrollTableView {
    
    if (!_scrollTableView) {
        _scrollTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, showRowsHeight * showRows) style:UITableViewStylePlain];
        _scrollTableView.dataSource = self;
        _scrollTableView.delegate = self;
        _scrollTableView.showsVerticalScrollIndicator = NO;
        _scrollTableView.showsHorizontalScrollIndicator = NO;
        _scrollTableView.scrollEnabled = NO;
    }
    return _scrollTableView;
}

- (void)removeTimer {
    
    [self.timer invalidate];
    self.timer = nil;
}

- (void)dealloc {
    
    [self.timer invalidate];
    self.timer = nil;
}

你可能感兴趣的:(无限向上滚动公告实现)