RDSHomeSceneView.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // RDSHomeSceneView.m
  3. // Temperature
  4. //
  5. // Created by RD on 2022/10/25.
  6. //
  7. #import "RDSHomeSceneView.h"
  8. #import "RDSHomeSceneCell.h"
  9. static NSString * const RDSHomeSceneCellID = @"RDSHomeSceneCellID";
  10. const NSInteger CellNumber = 3;
  11. @interface RDSHomeSceneView ()<UICollectionViewDelegate, UICollectionViewDataSource>
  12. @property (nonatomic, weak) UICollectionView *collectionView;
  13. @property (nonatomic, weak) UIPageControl *pageControl;
  14. @end
  15. @implementation RDSHomeSceneView
  16. - (instancetype)init
  17. {
  18. self = [super init];
  19. if (self) {
  20. self.backgroundColor = UIColor.clearColor;
  21. [self p_setupCollectionView];
  22. }
  23. return self;
  24. }
  25. - (void)p_setupCollectionView {
  26. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  27. CGFloat viewW = (SCREEN_WIDTH-20*2-24)/CellNumber;
  28. layout.itemSize = CGSizeMake(viewW, 48);
  29. layout.minimumLineSpacing = 12;
  30. layout.sectionInset = UIEdgeInsetsMake(0, 0, 20, 0);
  31. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  32. // UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
  33. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  34. collectionView.showsHorizontalScrollIndicator = NO;
  35. collectionView.backgroundColor = self.backgroundColor;
  36. [collectionView registerNib:[UINib nibWithNibName:@"RDSHomeSceneCell" bundle:nil] forCellWithReuseIdentifier:RDSHomeSceneCellID];
  37. collectionView.delegate = self;
  38. collectionView.dataSource = self;
  39. collectionView.pagingEnabled = YES;
  40. [self addSubview:collectionView];
  41. self.collectionView = collectionView;
  42. [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.top.equalTo(self);
  44. make.bottom.equalTo(self);
  45. make.left.equalTo(self).offset(20);
  46. make.right.equalTo(self).offset(-20);
  47. }];
  48. // UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(p_longPressMethod:)];
  49. // longPress.minimumPressDuration = 0.3f;
  50. // [collectionView addGestureRecognizer:longPress];
  51. }
  52. - (UIPageControl *)pageControl {
  53. if (_pageControl == nil) {
  54. UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 60, 10)];
  55. pageControl.currentPageIndicatorTintColor = RDSGreenColor;
  56. pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
  57. [self addSubview:pageControl];
  58. _pageControl = pageControl;
  59. }
  60. return _pageControl;
  61. }
  62. - (void)setScenes:(NSArray *)scenes {
  63. _scenes = scenes;
  64. int page = ceilf((scenes.count)/((float)CellNumber));
  65. if (page > 1) {
  66. self.pageControl.numberOfPages = page;
  67. self.pageControl.rds_width = page*30;
  68. [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
  69. make.centerX.equalTo(self);
  70. make.bottom.equalTo(self);
  71. }];
  72. // self.pageControl.rds_x = (self.rds_width-self.pageControl.rds_width)*0.5;
  73. // self.pageControl.rds_y = self.rds_height-20;
  74. }
  75. self.pageControl.hidden = page <= 1;
  76. [self.collectionView reloadData];
  77. }
  78. #pragma mark - UICollectionViewDelegate
  79. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  80. RDS_WEAKSELF(weakSelf)
  81. if (indexPath.row == _scenes.count) {
  82. if ([self.delegate respondsToSelector:@selector(rds_sceneSelViewDidSelAddItem:)]) {
  83. [self.delegate rds_sceneSelViewDidSelAddItem:weakSelf];
  84. }
  85. }
  86. else {
  87. // if ([self.delegate respondsToSelector:@selector(rds_sceneSelView:didSelectedScene:atIndexPath:)]) {
  88. // [self.delegate rds_sceneSelView:weakSelf didSelectedScene:_scenes[indexPath.row] atIndexPath:indexPath];
  89. // }
  90. // RDSSceneModel *scene = self.scenes[indexPath.row];
  91. // [[SigControlHelper sharedInstance] rds_devCtrSceneWithScene:scene];
  92. }
  93. }
  94. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  95. self.pageControl.currentPage = (int)round((scrollView.contentOffset.x/self.rds_width));
  96. }
  97. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  98. if (scrollView.contentOffset.x == roundf(scrollView.contentSize.width)-self.rds_width) {
  99. self.pageControl.currentPage = self.pageControl.numberOfPages-1;
  100. }
  101. }
  102. #pragma mark - UICollectionViewDataSource
  103. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  104. return _scenes.count; // +1为列表后面的添加按钮
  105. }
  106. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  107. RDSHomeSceneCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RDSHomeSceneCellID forIndexPath:indexPath];
  108. // if (indexPath.row == _scenes.count) {
  109. // cell.sceneIconView.image = [UIImage imageNamed:@"sceneAdd"];
  110. // cell.nameLab.text = @"添加";
  111. // }
  112. // else {
  113. // RDSSceneModel *scene = self.scenes[indexPath.row];
  114. // cell.sceneIconView.image = [UIImage imageNamed:[NSString stringWithFormat:@"sceneIcon_%d", scene.icon]];
  115. // cell.nameLab.text = scene.name;
  116. cell.nameLab.text = [NSString stringWithFormat:@"模式%@",_scenes[indexPath.row]];
  117. // }
  118. // cell.contentView.backgroundColor = [UIColor grayColor];
  119. return cell;
  120. }
  121. @end