无限向上滚动公告实现

思路:一般公告都是以文本为主,所以使用UIScrollView+UILabel便可以实现。由于项目需求,滚动公告一行需要展示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;//将数据传入

@end

.m文件

#import "NoticeScrollView.h"

const int  showRows = 5;//公告一次性展示的行数
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会停止
     这个方法会默认把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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,092评论 3 38
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,252评论 4 61
  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,721评论 2 7
  • (一) 重点提醒:人的格局高于所处位置,不一定是好事。或许从国际形势、地方政策、企业战略、管理科学角度你的做...
    我就是大怪兽阅读 303评论 0 4
  • 开发angular是一件愉快的事情,虽然总会有人诟病,但那又如何。你玩的开心就行了,就像小婴儿抓到一张也能玩的很嗨...
    葉糖糖阅读 933评论 2 2