NYChartMatrixView.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // NYChartMatrixView.m
  3. // NYChartDemo
  4. //
  5. // Created by kgj on 2023/3/17.
  6. //
  7. #import "NYChartMatrixView.h"
  8. #import "NYChartMatrixViewCell.h"
  9. @interface NYChartMatrixView ()
  10. {
  11. bool _isLoad;
  12. }
  13. //内容view
  14. @property (nonatomic,weak) UIView *contentView;
  15. @property (nonatomic,weak) UIScrollView *mScrollView;
  16. @property (nonatomic,weak) UIButton *currentBtn;
  17. @end
  18. @implementation NYChartMatrixView
  19. #pragma mark --UI
  20. - (instancetype)initWithFrame:(CGRect)frame
  21. {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  24. [self setupUI];
  25. }
  26. return self;
  27. }
  28. - (instancetype)initWithCoder:(NSCoder *)decoder
  29. {
  30. self = [super initWithCoder:decoder];
  31. if (self) {
  32. [self setupUI];
  33. }
  34. return self;
  35. }
  36. - (void)setupUI
  37. {
  38. // self.backgroundColor = UIColor.yellowColor;
  39. UIScrollView *mScrollView = [[UIScrollView alloc] init];
  40. [self addSubview:mScrollView];
  41. self.mScrollView = mScrollView;
  42. [mScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.top.bottom.mas_equalTo(self);
  44. make.left.mas_equalTo(self).offset(0);
  45. make.right.mas_equalTo(self).offset(0);
  46. }];
  47. UIView *contentView = [[UIView alloc] init];
  48. contentView.mj_w = 200.f;
  49. [mScrollView addSubview:contentView];
  50. self.contentView = contentView;
  51. // [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  52. // make.top.bottom.mas_equalTo(self);
  53. // make.left.mas_equalTo(self).offset(0);
  54. // make.right.mas_equalTo(self).offset(0);
  55. // }];
  56. }
  57. - (void)layoutSubviews
  58. {
  59. [super layoutSubviews];
  60. }
  61. #pragma mark --NYBaseChartViewDelegate
  62. - (void)loadView
  63. {
  64. if(!_isLoad){
  65. _isLoad = YES;
  66. /**
  67. 333 333 333 333
  68. 333 333 333 333
  69. */
  70. if(self.dataSource.count){
  71. NSInteger totalloc = 10;//总列
  72. CGFloat itemW = floorf(KRealValue(20));
  73. CGFloat itemH = floorf(KRealValue(22));
  74. CGFloat margin = 0.5;
  75. for (int i=0; i<self.dataSource.count; i++) {
  76. NSDictionary *dict = self.dataSource[i];
  77. NSString *title = dict[@"count"];
  78. NSInteger type = 0;
  79. NSUInteger loc = i / totalloc;//列
  80. NSUInteger row = i % totalloc;//行
  81. NSLog(@"loc = %zd row = %zd \n",loc,row);
  82. CGFloat itemX = (itemW+margin)*row;
  83. CGFloat itemY = (itemH+margin)*loc;
  84. NYChartMatrixViewCell *cell = [[NYChartMatrixViewCell alloc] initWithFrame:CGRectMake(itemX, itemY, itemW, itemH)];
  85. cell.tag = i+99;
  86. cell.itemButton.tag = i;
  87. cell.type = type;
  88. [cell.itemButton setTitle:title forState:UIControlStateNormal];
  89. [cell.itemButton setTitleColor:dict[@"color"] forState:UIControlStateNormal];
  90. [cell.itemButton addTarget:self action:@selector(itemClickdo:) forControlEvents:UIControlEventTouchUpInside];
  91. [self.contentView addSubview:cell];
  92. self.contentView.mj_h = cell.bottom+10.f;
  93. }
  94. self.mScrollView.contentSize = CGSizeMake(0, self.contentView.mj_h);
  95. }
  96. }else{
  97. [self updateView];
  98. }
  99. }
  100. - (void)updateView
  101. {
  102. if(self.dataSource.count){
  103. for (int i=0; i<self.dataSource.count; i++) {
  104. NSDictionary *dict = self.dataSource[i];
  105. NSString *title = dict[@"count"];
  106. NSInteger type = 0;
  107. NYChartMatrixViewCell *cell = [self.contentView viewWithTag:i+99];
  108. cell.type = type;
  109. [cell.itemButton setTitle:title forState:UIControlStateNormal];
  110. [cell.itemButton setTitleColor:dict[@"color"] forState:UIControlStateNormal];
  111. }
  112. }
  113. }
  114. - (void)setSelectIndex:(NSInteger)selectIndex
  115. {
  116. _selectIndex = selectIndex;
  117. self.currentBtn.selected = NO;
  118. NYChartMatrixViewCell *cell = [self.contentView viewWithTag:selectIndex+99];
  119. cell.itemButton.selected = YES;
  120. self.currentBtn = cell.itemButton;
  121. }
  122. //点击事件
  123. - (void)itemClickdo:(UIButton*)btn
  124. {
  125. self.currentBtn.selected = NO;
  126. NYChartMatrixViewCell *cell = [self.contentView viewWithTag:btn.tag+99];
  127. if(self.matrixItemClickBlock){
  128. self.matrixItemClickBlock(cell, (int)btn.tag);
  129. }
  130. btn.selected = YES;
  131. self.currentBtn = btn;
  132. }
  133. @end