cell实现跑马灯效果

来源:互联网 发布:明通网络数码科技 编辑:程序博客网 时间:2024/06/01 07:54
#import "ViewController.h"
 
@interfaceViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(weak, nonatomic)IBOutletUITableView *testTableView;
@property(weak, nonatomic)IBOutletNSLayoutConstraint *bottomLine;
 
@property(weak, nonatomic)IBOutletNSLayoutConstraint *topLine;
@property(weak, nonatomic)IBOutletNSLayoutConstraint *topLine2;
@property(weak, nonatomic)IBOutletNSLayoutConstraint *bottomLine2;
 
 
@property(strong, nonatomic) CADisplayLink *displayLink;
 
 
@property(assign,nonatomic)intcount;
 
@property(strong,nonatomic)NSArray*dataArray;
@end
 
@implementationViewController
 
- (void)viewDidLoad {
    [superviewDidLoad];
 
    self.count = 0;
    self.testTableView.delegate = self;
    self.testTableView.dataSource = self;
 
    self.displayLink = [CADisplayLink displayLinkWithTarget:selfselector:@selector(tick:)];
    [self.displayLink addToRunLoop:[NSRunLoopcurrentRunLoop]
                           forMode:NSDefaultRunLoopMode];
    //测试数据----->将需要展示的数据进行拼接,比如需要展示的数据数组为 @[@"1",@"2",@"3",@"4",@"5"] 那么需要拼接新数组 为 @[@"1",@"2",@"3",@"4",@"5",@"1",@"2",@"3",@"4",@"5"],示例如下
    self.dataArray = [NSArrayarrayWithObjects:@"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 {
    [superdidReceiveMemoryWarning];
}
 
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 
    returnself.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];
    returncell;
}
 
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
 
    return50;
}
 
- (void)dealloc {
 
    [self.displayLink invalidate];
    self.displayLink = nil;
 
}
 
@end
0 0
原创粉丝点击