NYChartMatrixView.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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(19));
  63. CGFloat itemH = floorf(KRealValue(22));
  64. CGFloat margin = KRealValue(1);
  65. for (int i=0; i<self.dataSource.count; i++) {
  66. NSDictionary *dict = self.dataSource[i];
  67. NSString *title = dict[@"count"];
  68. NSInteger type = [dict[@"color"] intValue];
  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 addTarget:self action:@selector(itemClickdo:) forControlEvents:UIControlEventTouchUpInside];
  80. [self.contentView addSubview:cell];
  81. }
  82. }
  83. }else{
  84. [self updateView];
  85. }
  86. }
  87. - (void)updateView
  88. {
  89. if(self.dataSource.count){
  90. for (int i=0; i<self.dataSource.count; i++) {
  91. NSDictionary *dict = self.dataSource[i];
  92. NSString *title = dict[@"count"];
  93. NSInteger type = [dict[@"color"] intValue];
  94. NYChartMatrixViewCell *cell = [self.contentView viewWithTag:i+99];
  95. cell.type = type;
  96. [cell.itemButton setTitle:title forState:UIControlStateNormal];
  97. }
  98. }
  99. }
  100. - (void)setSelectIndex:(NSInteger)selectIndex
  101. {
  102. _selectIndex = selectIndex;
  103. self.currentBtn.selected = NO;
  104. NYChartMatrixViewCell *cell = [self.contentView viewWithTag:selectIndex+99];
  105. cell.itemButton.selected = YES;
  106. self.currentBtn = cell.itemButton;
  107. }
  108. //点击事件
  109. - (void)itemClickdo:(UIButton*)btn
  110. {
  111. self.currentBtn.selected = NO;
  112. NYChartMatrixViewCell *cell = [self.contentView viewWithTag:btn.tag+99];
  113. if(self.matrixItemClickBlock){
  114. self.matrixItemClickBlock(cell, (int)btn.tag);
  115. }
  116. btn.selected = YES;
  117. self.currentBtn = btn;
  118. }
  119. @end