RDSHomeRoomView.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // RDSHomeRoomView.m
  3. // Temperature
  4. //
  5. // Created by RD on 2022/10/31.
  6. //
  7. #import "RDSHomeRoomView.h"
  8. #import "RDSRoomCell.h"
  9. #import "RDSHomeTopView.h"
  10. #import "RDSHomeSceneView.h"
  11. #import "RDSAddRoomView.h"
  12. /*
  13. UICollectionView header吸顶悬停
  14. 设置两个section。第一组的section的footer来充当整个UICollectionView的header效果划出;然后设置第二组的header,设置正常的items,UICollectionView 的flowLayout的sectionHeadersPinToVisibleBounds属性设置为true则有悬停效果。
  15. 作者:落夏简叶
  16. 链接:http://events.jianshu.io/p/dd1e87795b63
  17. */
  18. static NSString * const RDSRoomCellID = @"RDSRoomCellID";
  19. @interface RDSHomeRoomView ()<UICollectionViewDelegate, UICollectionViewDataSource>
  20. @property (nonatomic, weak) UICollectionView *collectionView;
  21. @property (nonatomic, strong) UICollectionReusableView *footerView;
  22. @property (nonatomic, strong) UICollectionReusableView *headerView;
  23. @property (nonatomic, strong) RDSHomeTopView *topView;
  24. @property (nonatomic, strong) RDSHomeSceneView *sceneView;
  25. @property (nonatomic, strong) RDSAddRoomView *addView;
  26. @end
  27. @implementation RDSHomeRoomView
  28. - (instancetype)init
  29. {
  30. self = [super init];
  31. if (self) {
  32. self.backgroundColor = RDSViewBgColor;
  33. [self p_setupCollectionView];
  34. }
  35. return self;
  36. }
  37. - (void)p_setupCollectionView {
  38. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  39. CGFloat viewW = (SCREEN_WIDTH-20*2-10)/2;
  40. layout.itemSize = CGSizeMake(viewW, viewW);
  41. layout.minimumLineSpacing = 15;
  42. layout.sectionInset = UIEdgeInsetsMake(0, 20, 20, 20);
  43. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  44. layout.sectionHeadersPinToVisibleBounds = YES;
  45. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  46. collectionView.showsHorizontalScrollIndicator = NO;
  47. collectionView.showsVerticalScrollIndicator = NO;
  48. collectionView.backgroundColor = self.backgroundColor;
  49. [collectionView registerNib:[UINib nibWithNibName:@"RDSRoomCell" bundle:nil] forCellWithReuseIdentifier:RDSRoomCellID];
  50. collectionView.delegate = self;
  51. collectionView.dataSource = self;
  52. collectionView.pagingEnabled = NO;
  53. [self addSubview:collectionView];
  54. self.collectionView = collectionView;
  55. [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  56. make.top.equalTo(self);
  57. make.bottom.equalTo(self);
  58. make.left.equalTo(self);//.offset(20);
  59. make.right.equalTo(self);//.offset(-20);
  60. }];
  61. //注册行头
  62. // [self.collectView registerNib:[UINib nibWithNibName:@"NibName" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReuseIdentifier"];
  63. //或者
  64. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
  65. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];
  66. // UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(p_longPressMethod:)];
  67. // longPress.minimumPressDuration = 0.3f;
  68. // [collectionView addGestureRecognizer:longPress];
  69. }
  70. - (void)reloadData{
  71. [self.collectionView reloadData];
  72. self.addView.hidden = NO;
  73. }
  74. #pragma mark - UICollectionViewDelegate
  75. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  76. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  77. // RDS_WEAKSELF(weakSelf)
  78. // if (indexPath.row == _scenes.count) {
  79. // if ([self.delegate respondsToSelector:@selector(rds_sceneSelViewDidSelAddItem:)]) {
  80. // [self.delegate rds_sceneSelViewDidSelAddItem:weakSelf];
  81. // }
  82. // }
  83. // else {
  84. //
  85. //
  86. // }
  87. }
  88. #pragma mark - UICollectionViewDataSource
  89. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  90. return 2;
  91. }
  92. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  93. if(section == 0){
  94. return 0;
  95. }
  96. return 0;//_rooms.count;
  97. }
  98. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  99. RDSRoomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RDSRoomCellID forIndexPath:indexPath];
  100. return cell;
  101. }
  102. // 不允许高亮
  103. - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
  104. return NO;
  105. }
  106. // 设置header尺寸
  107. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  108. if(section == 0){
  109. return CGSizeZero;
  110. }
  111. return CGSizeMake(SCREEN_WIDTH, 50);
  112. }
  113. // 设置footer尺寸
  114. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
  115. if(section == 0){
  116. return CGSizeMake(SCREEN_WIDTH, 306-60);// -的是场景的高度,场景先不做
  117. }
  118. return CGSizeZero;
  119. }
  120. //sectionheader
  121. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  122. if (kind == UICollectionElementKindSectionFooter) {
  123. UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer" forIndexPath:indexPath];
  124. footerView.backgroundColor = UIColor.clearColor;
  125. if(self.topView == nil){
  126. self.footerView = footerView;
  127. [self p_setupTopView];
  128. //[self p_setupSceneView]; 先不做场景功能
  129. }
  130. return footerView;
  131. }
  132. UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
  133. headView.backgroundColor = RDSViewBgColor;
  134. UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 20)];
  135. title.text = @"我的房间";
  136. title.font = [UIFont boldSystemFontOfSize:18];
  137. [headView addSubview:title];
  138. return headView;
  139. }
  140. // 顶部视图
  141. - (void)p_setupTopView{
  142. // 375 217.5
  143. CGFloat viewH = 206;//kSCALE_WIDTH_6*222;
  144. RDSHomeTopView *view = [RDSHomeTopView rds_loadViewFromNib];
  145. view.frame = CGRectMake(0, 0, SCREEN_WIDTH, viewH);
  146. [self.footerView addSubview:view];
  147. [view mas_makeConstraints:^(MASConstraintMaker *make) {
  148. make.top.equalTo(self.footerView);
  149. make.height.equalTo(@(viewH));
  150. make.left.equalTo(self.footerView);
  151. make.right.equalTo(self.footerView);
  152. }];
  153. self.topView = view;
  154. }
  155. - (void)p_setupSceneView{
  156. RDSHomeSceneView *scenesView = [[RDSHomeSceneView alloc] init];
  157. [self.footerView addSubview:scenesView];
  158. self.sceneView = scenesView;
  159. //scenesView.delegate = self;
  160. [scenesView mas_makeConstraints:^(MASConstraintMaker *make) {
  161. make.top.equalTo(self.topView.mas_bottom).offset(10);
  162. make.height.equalTo(@90);
  163. make.left.equalTo(self.footerView);//.offset(10);
  164. make.right.equalTo(self.footerView);//.offset(-10);
  165. }];
  166. NSArray *scenes = @[@1,@2,@3,@4,@5,@6];
  167. scenesView.scenes = scenes;
  168. }
  169. - (RDSAddRoomView *)addView{
  170. if(_addView == nil){
  171. RDS_WEAKSELF(weakSelf)
  172. _addView = [RDSAddRoomView rds_loadViewFromNib];
  173. _addView.hidden = YES;
  174. [self.collectionView addSubview:_addView];
  175. [_addView mas_makeConstraints:^(MASConstraintMaker *make) {
  176. make.left.equalTo(self);//.offset(20);
  177. make.right.equalTo(self);//.offset(-20);
  178. make.height.equalTo(@300);
  179. make.top.equalTo(@(SCREEN_HEIGHT*0.4));
  180. }];
  181. }
  182. return _addView;
  183. }
  184. @end