XMGRoolLinghtVIew.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // XMGRoolLinghtVIew.m
  3. // testTableVIew
  4. //
  5. // Created by 123 on 2020/4/30.
  6. // Copyright © 2020 DR. All rights reserved.
  7. //
  8. #import "XMGRoolLinghtVIew.h"
  9. @interface XMGRoolLinghtVIew()<UITableViewDelegate,UITableViewDataSource>
  10. @property (weak, nonatomic) IBOutlet UITableView *tableView;
  11. @property (strong, nonatomic) CADisplayLink *displayLink;
  12. @property (assign,nonatomic) int count;
  13. @end
  14. @implementation XMGRoolLinghtVIew
  15. -(void)setDataArr:(NSMutableArray *)dataArr{
  16. _dataArr=dataArr;
  17. [self.tableView reloadData];
  18. }
  19. -(void)awakeFromNib{
  20. [super awakeFromNib];
  21. self.count = 0;
  22. _dataArr=[NSMutableArray array];
  23. self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
  24. self.tableView.delegate=self;
  25. self.tableView.dataSource=self;
  26. self.layer.cornerRadius=5;
  27. self.layer.masksToBounds=YES;
  28. self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
  29. [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop]
  30. forMode:NSDefaultRunLoopMode];
  31. //测试数据----->将需要展示的数据进行拼接,比如需要展示的数据数组为 @[@"1",@"2",@"3",@"4",@"5"] 那么需要拼接新数组 为 @[@"1",@"2",@"3",@"4",@"5",@"1",@"2",@"3",@"4",@"5"],示例如下
  32. [self.tableView setContentOffset:CGPointMake(0, 10) animated:YES];
  33. self.tableView.userInteractionEnabled = NO;
  34. //_dataArr = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"1",@"2",@"3",@"4",@"5", nil];
  35. }
  36. +(instancetype)XIBXMGRoolLinghtView{
  37. return [[NSBundle mainBundle] loadNibNamed:@"XMGRoolLinghtVIew" owner:nil options:nil].firstObject;
  38. }
  39. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  40. return 30;
  41. }
  42. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  43. return _dataArr.count;
  44. }
  45. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  46. packageRoolCell *cell = [packageRoolCell cellWithTableView:tableView];
  47. packageRoolModel *model=_dataArr[indexPath.row];
  48. cell.model=model;
  49. return cell;
  50. }
  51. //CADisplayLink 定时器 系统默认每秒调用60次
  52. - (void) tick:(CADisplayLink *)displayLink {
  53. self.count ++;
  54. //(25.0 / 30.0) * (float)self.count) ---> (tableview需要滚动的contentOffset / 一共调用的次数) * 第几次调用
  55. //比如该demo中 contentOffset最大值为 = cell的高度 * cell的个数 ,5秒执行一个循环则调用次数为 300,没1/60秒 count计数器加1,当count=300时,重置count为0,实现循环滚动.
  56. [self.tableView setContentOffset:CGPointMake(0, ((15.0 / 30.0) * (float)self.count)) animated:NO];
  57. if (self.count >= 150) {
  58. self.count = 0;
  59. }
  60. }
  61. - (void)dealloc {
  62. [self.displayLink invalidate];
  63. self.displayLink = nil;
  64. }
  65. @end