项目介绍:
上下滚动的跑马灯,数据上下循环滚动
[Objective-C] 查看源文件 复制代码
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *testTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomLine;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLine;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLine2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomLine2;
@property (strong, nonatomic) CADisplayLink *displayLink;
@property (assign,nonatomic) int count;
@property (strong,nonatomic) NSArray *dataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.count = 0;
self.testTableView.delegate = self;
self.testTableView.dataSource = self;
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
//测试数据----->将需要展示的数据进行拼接,比如需要展示的数据数组为 @[@"1",@"2",@"3",@"4",@"5"] 那么需要拼接新数组 为 @[@"1",@"2",@"3",@"4",@"5",@"1",@"2",@"3",@"4",@"5"],示例如下
self.dataArray = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"1",@"2",@"3",@"4",@"5", nil];
[self.testTableView setContentOffset:CGPointMake(0, 0) animated:YES];
self.testTableView.userInteractionEnabled = NO;
}
//CADisplayLink 定时器 系统默认每秒调用60次
- (void) tick:(CADisplayLink *)displayLink {
self.count ++;
//(25.0 / 30.0) * (float)self.count) ---> (tableview需要滚动的contentOffset / 一共调用的次数) * 第几次调用
//比如该demo中 contentOffset最大值为 = cell的高度 * cell的个数 ,5秒执行一个循环则调用次数为 300,没1/60秒 count计数器加1,当count=300时,重置count为0,实现循环滚动.
[self.testTableView setContentOffset:CGPointMake(0, ((25.0 / 30.0) * (float)self.count)) animated:NO];
if (self.count >= 300) {
self.count = 0;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = self.dataArray[indexPath.row];
cell.backgroundColor = [UIColor clearColor];
return cell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
- (void)dealloc {
[self.displayLink invalidate];
self.displayLink = nil;
}
@end
DEMO直接下载:

CCPScrollTableVIew .zip
(47.85 KB, 下载次数: 472, 售价: 10 金钱)
2016-7-12 09:54 上传
点击文件名下载附件
