NYChartMatrixView.m 3.7 KB

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