cell实现跑马灯效果

#import "ViewController.h"
 
@interface ViewController ()
@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

你可能感兴趣的:(cell实现跑马灯)