RDSCtrlModeView.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // RDSCtrlModeView.m
  3. // Temperature
  4. //
  5. // Created by RD on 2023/1/6.
  6. //
  7. #import "RDSCtrlModeView.h"
  8. #import "RDSCtrlModeCell.h"
  9. static NSString * const RDSCtrlModeCellID = @"RDSCtrlModeCellID";
  10. const NSInteger ModeCellNumber = 4;
  11. @interface RDSCtrlModeView ()<UICollectionViewDelegate, UICollectionViewDataSource>
  12. @property (nonatomic, weak) UICollectionView *collectionView;
  13. @property (nonatomic, strong) NSMutableArray <ModelImg *> *sources;
  14. @property (nonatomic, assign) int lastMode;
  15. @end
  16. @implementation RDSCtrlModeView
  17. - (instancetype)init
  18. {
  19. self = [super init];
  20. if (self) {
  21. self.backgroundColor = UIColor.clearColor;
  22. [self p_setupSources];
  23. [self p_setupCollectionView];
  24. }
  25. return self;
  26. }
  27. - (void)p_setupSources{
  28. _sources = [NSMutableArray array];
  29. // 工作模式(0:制冷,1:制热,2:除湿3:送风4:加湿)
  30. ModelImg *m1 = [[ModelImg alloc] init];
  31. m1.img = @"icon_refrigeration";
  32. m1.imgSel = @"icon_refrigeration_sel";
  33. ModelImg *m2 = [[ModelImg alloc] init];
  34. m2.img = @"icon_heating";
  35. m2.imgSel = @"icon_heating_sel";
  36. ModelImg *m3 = [[ModelImg alloc] init];
  37. m3.img = @"icon_dehumidification";
  38. m3.imgSel = @"icon_dehumidification_sel";
  39. ModelImg *m4 = [[ModelImg alloc] init];
  40. m4.img = @"icon_wind";
  41. m4.imgSel = @"icon_wind_sel";
  42. ModelImg *m5 = [[ModelImg alloc] init];
  43. m5.img = @"icon_humidify";
  44. m5.imgSel = @"icon_humidify_sel";
  45. // ModelImg *m6 = [[ModelImg alloc] init];
  46. // m6.img = @"icon_humidify";
  47. // m6.imgSel = @"icon_humidify_sel";
  48. [_sources addObject:m1];
  49. [_sources addObject:m2];
  50. [_sources addObject:m3];
  51. [_sources addObject:m4];
  52. [_sources addObject:m5];
  53. // [_sources addObject:m6];
  54. }
  55. - (void)setMode:(int)mode{
  56. if(mode != _lastMode){
  57. _lastMode = mode;
  58. dispatch_async(dispatch_get_main_queue(), ^{
  59. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:mode inSection:0];
  60. [self.collectionView reloadData];
  61. [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:true];
  62. });
  63. }
  64. }
  65. - (void)p_setupCollectionView {
  66. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  67. CGFloat viewW = (SCREEN_WIDTH-20*(ModeCellNumber-1+2))/ModeCellNumber;
  68. layout.itemSize = CGSizeMake(viewW, viewW);
  69. layout.minimumLineSpacing = 20;
  70. layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
  71. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  72. // UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
  73. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  74. collectionView.showsHorizontalScrollIndicator = NO;
  75. collectionView.backgroundColor = self.backgroundColor;
  76. [collectionView registerNib:[UINib nibWithNibName:@"RDSCtrlModeCell" bundle:nil] forCellWithReuseIdentifier:RDSCtrlModeCellID];
  77. collectionView.delegate = self;
  78. collectionView.dataSource = self;
  79. collectionView.pagingEnabled = YES;
  80. [self addSubview:collectionView];
  81. self.collectionView = collectionView;
  82. [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.top.equalTo(self);
  84. make.bottom.equalTo(self);
  85. make.left.equalTo(self).offset(20);
  86. make.right.equalTo(self).offset(-20);
  87. }];
  88. }
  89. #pragma mark - UICollectionViewDelegate
  90. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  91. int curSelect = (int)indexPath.row;
  92. if(curSelect == _lastMode){// 和上次点击模式相同
  93. return;
  94. }else{// 点击其它模式
  95. _lastMode = curSelect;
  96. }
  97. [collectionView reloadData];
  98. [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:true];
  99. BLOCK_SAFE_RUN(_onModeClick,_lastMode)
  100. }
  101. #pragma mark - UICollectionViewDataSource
  102. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  103. return _sources.count;
  104. }
  105. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  106. RDSCtrlModeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RDSCtrlModeCellID forIndexPath:indexPath];
  107. cell.tag = indexPath.row;
  108. cell.modelImg = _sources[indexPath.row];
  109. cell.layer.masksToBounds = YES;
  110. cell.layer.cornerRadius = 10;
  111. cell.mode = _lastMode;
  112. return cell;
  113. }
  114. @end