123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- //
- // NYChartMatrixView.m
- // NYChartDemo
- //
- // Created by kgj on 2023/3/17.
- //
- #import "NYChartMatrixView.h"
- #import "NYChartMatrixViewCell.h"
- @interface NYChartMatrixView ()
- {
- bool _isLoad;
- }
- //内容view
- @property (nonatomic,weak) UIView *contentView;
- @property (nonatomic,weak) UIScrollView *mScrollView;
- @property (nonatomic,weak) UIButton *currentBtn;
- @end
- @implementation NYChartMatrixView
- #pragma mark --UI
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self setupUI];
- }
- return self;
- }
- - (instancetype)initWithCoder:(NSCoder *)decoder
- {
- self = [super initWithCoder:decoder];
- if (self) {
- [self setupUI];
- }
- return self;
- }
- - (void)setupUI
- {
- // self.backgroundColor = UIColor.yellowColor;
-
- UIScrollView *mScrollView = [[UIScrollView alloc] init];
- [self addSubview:mScrollView];
- self.mScrollView = mScrollView;
- [mScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.bottom.mas_equalTo(self);
- make.left.mas_equalTo(self).offset(0);
- make.right.mas_equalTo(self).offset(0);
- }];
-
- UIView *contentView = [[UIView alloc] init];
- contentView.mj_w = 200.f;
- [mScrollView addSubview:contentView];
- self.contentView = contentView;
- // [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
- // make.top.bottom.mas_equalTo(self);
- // make.left.mas_equalTo(self).offset(0);
- // make.right.mas_equalTo(self).offset(0);
- // }];
-
- }
- - (void)layoutSubviews
- {
- [super layoutSubviews];
- }
- #pragma mark --NYBaseChartViewDelegate
- - (void)loadView
- {
- if(!_isLoad){
- _isLoad = YES;
- /**
- 333 333 333 333
- 333 333 333 333
- */
- if(self.dataSource.count){
- NSInteger totalloc = 10;//总列
- CGFloat itemW = floorf(KRealValue(20));
- CGFloat itemH = floorf(KRealValue(22));
- CGFloat margin = 0.5;
- for (int i=0; i<self.dataSource.count; i++) {
- NSDictionary *dict = self.dataSource[i];
- NSString *title = dict[@"count"];
- NSInteger type = 0;
- NSUInteger loc = i / totalloc;//列
- NSUInteger row = i % totalloc;//行
- NSLog(@"loc = %zd row = %zd \n",loc,row);
- CGFloat itemX = (itemW+margin)*row;
- CGFloat itemY = (itemH+margin)*loc;
- NYChartMatrixViewCell *cell = [[NYChartMatrixViewCell alloc] initWithFrame:CGRectMake(itemX, itemY, itemW, itemH)];
- cell.tag = i+99;
- cell.itemButton.tag = i;
- cell.type = type;
- [cell.itemButton setTitle:title forState:UIControlStateNormal];
- [cell.itemButton setTitleColor:dict[@"color"] forState:UIControlStateNormal];
- [cell.itemButton addTarget:self action:@selector(itemClickdo:) forControlEvents:UIControlEventTouchUpInside];
- [self.contentView addSubview:cell];
- self.contentView.mj_h = cell.bottom+10.f;
- }
- self.mScrollView.contentSize = CGSizeMake(0, self.contentView.mj_h);
- }
- }else{
- [self updateView];
- }
-
- }
- - (void)updateView
- {
- if(self.dataSource.count){
- for (int i=0; i<self.dataSource.count; i++) {
- NSDictionary *dict = self.dataSource[i];
- NSString *title = dict[@"count"];
- NSInteger type = 0;
- NYChartMatrixViewCell *cell = [self.contentView viewWithTag:i+99];
- cell.type = type;
- [cell.itemButton setTitle:title forState:UIControlStateNormal];
- [cell.itemButton setTitleColor:dict[@"color"] forState:UIControlStateNormal];
- }
- }
- }
- - (void)setSelectIndex:(NSInteger)selectIndex
- {
- _selectIndex = selectIndex;
- self.currentBtn.selected = NO;
- NYChartMatrixViewCell *cell = [self.contentView viewWithTag:selectIndex+99];
- cell.itemButton.selected = YES;
- self.currentBtn = cell.itemButton;
- }
- //点击事件
- - (void)itemClickdo:(UIButton*)btn
- {
- self.currentBtn.selected = NO;
- NYChartMatrixViewCell *cell = [self.contentView viewWithTag:btn.tag+99];
- if(self.matrixItemClickBlock){
- self.matrixItemClickBlock(cell, (int)btn.tag);
- }
- btn.selected = YES;
- self.currentBtn = btn;
- }
- @end
|