YMPersonalPageGiftWallView.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // YMPersonalPageGiftWallView.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/3/14.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMPersonalPageGiftWallView.h"
  9. #import "YMPersonalPageViewModel.h"
  10. #import "YMPersonalPageGiftWallCell.h"
  11. #import "CHTCollectionViewWaterfallLayout.h"
  12. @interface YMPersonalPageGiftWallView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,CHTCollectionViewDelegateWaterfallLayout>
  13. /// 个人主页VM
  14. @property (nonatomic, strong) YMPersonalPageViewModel *viewModel;
  15. /// 个人主页礼物墙标签
  16. @property (nonatomic, strong) UILabel *personalPageGiftWallLb;
  17. /// 动态排版
  18. @property (nonatomic, strong) CHTCollectionViewWaterfallLayout *giftWallLayout;
  19. /// 动态容器列表
  20. @property (nonatomic, strong) UICollectionView *giftWallCollectionView;
  21. @end
  22. @implementation YMPersonalPageGiftWallView
  23. - (void)ym_setupViews{
  24. [self addSubview:self.personalPageGiftWallLb];
  25. [self addSubview:self.giftWallCollectionView];
  26. [self setNeedsUpdateConstraints];
  27. [self updateConstraintsIfNeeded];
  28. }
  29. - (void)updateConstraints{
  30. [self.personalPageGiftWallLb mas_makeConstraints:^(MASConstraintMaker *make) {
  31. make.top.equalTo(self).offset(adapt(10));
  32. make.left.equalTo(self).offset(adapt(20));
  33. }];
  34. [self.giftWallCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.top.equalTo(self.personalPageGiftWallLb.mas_bottom).offset(adapt(20));
  36. make.left.equalTo(self).offset(adapt(20));
  37. make.right.equalTo(self).offset(adapt(-20));
  38. make.bottom.equalTo(self).offset(adapt(-10));
  39. }];
  40. [super updateConstraints];
  41. }
  42. - (void)ym_bindViewModel:(YMPersonalPageViewModel *)viewModel{
  43. if (!viewModel) {
  44. return;
  45. }
  46. _viewModel = viewModel;
  47. @weakify(self)
  48. [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id result) {
  49. @strongify(self)
  50. [self.giftWallCollectionView reloadData];
  51. [self.giftWallCollectionView layoutIfNeeded];
  52. //刷新高度
  53. CGFloat giftWallCollectionViewHeight = self.giftWallCollectionView.collectionViewLayout.collectionViewContentSize.height;
  54. [self.giftWallCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
  55. make.height.mas_equalTo(giftWallCollectionViewHeight);
  56. }];
  57. }];
  58. }
  59. #pragma mark - UICollectionViewDataSource
  60. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  61. return 1;
  62. }
  63. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  64. return self.viewModel.giftWallDataArray.count;
  65. }
  66. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  67. YMPersonalPageGiftWallCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMPersonalPageGiftWallCell class]) forIndexPath:indexPath];
  68. [cell ym_bindViewModel:self.viewModel.giftWallDataArray[indexPath.item]];
  69. return cell;
  70. }
  71. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  72. UICollectionReusableView *reusableView = nil;
  73. reusableView.backgroundColor = [UIColor clearColor];
  74. return reusableView;
  75. }
  76. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  77. return CGSizeZero;
  78. }
  79. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
  80. return CGSizeZero;
  81. }
  82. #pragma mark - UICollectionViewDelegate
  83. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  84. }
  85. #pragma mark - CHTCollectionViewDelegateWaterfallLayout
  86. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  87. return CGSizeMake(adapt(60), adapt(100));
  88. }
  89. - (UILabel *)personalPageGiftWallLb{
  90. if (!_personalPageGiftWallLb) {
  91. _personalPageGiftWallLb = [[UILabel alloc]init];
  92. _personalPageGiftWallLb.font = LCBoldFont(17);
  93. _personalPageGiftWallLb.textColor = HexColorFromRGB(0x000000);
  94. _personalPageGiftWallLb.textAlignment = NSTextAlignmentLeft;
  95. _personalPageGiftWallLb.text = @"TA的礼物";
  96. }
  97. return _personalPageGiftWallLb;
  98. }
  99. - (CHTCollectionViewWaterfallLayout *)giftWallLayout{
  100. if (!_giftWallLayout) {
  101. _giftWallLayout = [[CHTCollectionViewWaterfallLayout alloc] init];
  102. _giftWallLayout.columnCount = 5;
  103. _giftWallLayout.sectionInset = UIEdgeInsetsZero;
  104. }
  105. return _giftWallLayout;
  106. }
  107. - (UICollectionView *)giftWallCollectionView{
  108. if (!_giftWallCollectionView) {
  109. _giftWallCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.giftWallLayout];
  110. _giftWallCollectionView.delegate = self;
  111. _giftWallCollectionView.dataSource = self;
  112. _giftWallCollectionView.userInteractionEnabled = NO;
  113. _giftWallCollectionView.showsVerticalScrollIndicator = NO;
  114. _giftWallCollectionView.showsHorizontalScrollIndicator = NO;
  115. _giftWallCollectionView.backgroundColor = UIColor.whiteColor;
  116. [_giftWallCollectionView registerClass:[YMPersonalPageGiftWallCell class] forCellWithReuseIdentifier:NSStringFromClass([YMPersonalPageGiftWallCell class])];
  117. }
  118. return _giftWallCollectionView;
  119. }
  120. @end