// // RDSCtrlModeView.m // Temperature // // Created by RD on 2023/1/6. // #import "RDSCtrlModeView.h" #import "RDSCtrlModeCell.h" static NSString * const RDSCtrlModeCellID = @"RDSCtrlModeCellID"; const NSInteger ModeCellNumber = 4; @interface RDSCtrlModeView () @property (nonatomic, weak) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *sources; @property (nonatomic, assign) int lastMode; @end @implementation RDSCtrlModeView - (instancetype)init { self = [super init]; if (self) { self.backgroundColor = UIColor.clearColor; [self p_setupSources]; [self p_setupCollectionView]; } return self; } - (void)p_setupSources{ _sources = [NSMutableArray array]; // 工作模式(0:制冷,1:制热,2:除湿3:送风4:加湿) ModelImg *m1 = [[ModelImg alloc] init]; m1.img = @"icon_refrigeration"; m1.imgSel = @"icon_refrigeration_sel"; ModelImg *m2 = [[ModelImg alloc] init]; m2.img = @"icon_heating"; m2.imgSel = @"icon_heating_sel"; ModelImg *m3 = [[ModelImg alloc] init]; m3.img = @"icon_dehumidification"; m3.imgSel = @"icon_dehumidification_sel"; ModelImg *m4 = [[ModelImg alloc] init]; m4.img = @"icon_wind"; m4.imgSel = @"icon_wind_sel"; ModelImg *m5 = [[ModelImg alloc] init]; m5.img = @"icon_humidify"; m5.imgSel = @"icon_humidify_sel"; // ModelImg *m6 = [[ModelImg alloc] init]; // m6.img = @"icon_humidify"; // m6.imgSel = @"icon_humidify_sel"; [_sources addObject:m1]; [_sources addObject:m2]; [_sources addObject:m3]; [_sources addObject:m4]; [_sources addObject:m5]; // [_sources addObject:m6]; } - (void)setMode:(int)mode{ if(mode != _lastMode){ _lastMode = mode; dispatch_async(dispatch_get_main_queue(), ^{ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:mode inSection:0]; [self.collectionView reloadData]; [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:true]; }); } } - (void)p_setupCollectionView { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; CGFloat viewW = (SCREEN_WIDTH-20*(ModeCellNumber-1+2))/ModeCellNumber; layout.itemSize = CGSizeMake(viewW, viewW); layout.minimumLineSpacing = 20; layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0); layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; // UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout]; UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; collectionView.showsHorizontalScrollIndicator = NO; collectionView.backgroundColor = self.backgroundColor; [collectionView registerNib:[UINib nibWithNibName:@"RDSCtrlModeCell" bundle:nil] forCellWithReuseIdentifier:RDSCtrlModeCellID]; collectionView.delegate = self; collectionView.dataSource = self; collectionView.pagingEnabled = YES; [self addSubview:collectionView]; self.collectionView = collectionView; [collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self); make.bottom.equalTo(self); make.left.equalTo(self).offset(20); make.right.equalTo(self).offset(-20); }]; } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { int curSelect = (int)indexPath.row; if(curSelect == _lastMode){// 和上次点击模式相同 return; }else{// 点击其它模式 _lastMode = curSelect; } [collectionView reloadData]; [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:true]; BLOCK_SAFE_RUN(_onModeClick,_lastMode) } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _sources.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { RDSCtrlModeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RDSCtrlModeCellID forIndexPath:indexPath]; cell.tag = indexPath.row; cell.modelImg = _sources[indexPath.row]; cell.layer.masksToBounds = YES; cell.layer.cornerRadius = 10; cell.mode = _lastMode; return cell; } @end