思路:一般公告都是以文本為主,所以使用UIScrollView+UILabel便可以實(shí)現(xiàn)。由于項(xiàng)目需求,滾動公告一行需要展示4個UILabel,第四個UILabel的textColor不一樣,且一次展示多行,效果如下圖,所以考慮使用UIScrollView+UITableView。

公告滾動圖
.h文件
#import <UIKit/UIKit.h>
@interface NoticeScrollView : UIView
@property (strong, nonatomic) UIScrollView *MainScrollView;
@property (nonatomic, strong) UITableView *scrollTableView;
@property (nonatomic, copy) NSMutableArray *dataArr;//將數(shù)據(jù)傳入
@end
.m文件
#import "NoticeScrollView.h"
const int showRows = 5;//公告一次性展示的行數(shù)
const float showRowsHeight = 44; //公告每行的高度
@interface NoticeScrollView ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
/* 定時器 */
@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會停止
這個方法會默認(rèn)把Timer以NSDefaultRunLoopMode添加到主Runloop上,而當(dāng)你滑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;
}