RoomFaceView.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // RoomFaceView.m
  3. // UniversalApp
  4. //
  5. // Created by bogokj on 2019/8/27.
  6. // Copyright © 2019 voidcat. All rights reserved.
  7. //
  8. #import "RoomFaceView.h"
  9. #import "RoomFaceModel.h"
  10. #import "RoomFaceCell.h"
  11. #import "HorizontalLayout.h"
  12. @interface RoomFaceView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  13. @property(nonatomic, strong) UICollectionView *collectionView;
  14. @property(nonatomic, strong) NSMutableArray *dataArray;
  15. @property(nonatomic, strong) UIPageControl *pageControl;
  16. @property(nonatomic, strong) UIView *bgView;
  17. @property(nonatomic, strong) UIButton *selectedBtn;
  18. @property(nonatomic, strong) UIButton *face1Btn;
  19. @property(nonatomic, strong) NSString *face1_image_name;
  20. @end
  21. @implementation RoomFaceView
  22. - (instancetype)initWithFrame:(CGRect)frame{
  23. if (self = [super initWithFrame:frame]) {
  24. self.backgroundColor = [UIColor colorWithHexString:@"#202038"];
  25. [self addSubview:self.collectionView];
  26. [self addSubview:self.pageControl];
  27. [self addSubview:self.bgView];
  28. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  29. make.left.right.equalTo(self);
  30. make.top.mas_equalTo(self.bgView.mas_bottom);
  31. make.bottom.equalTo(self).offset(-20);
  32. }];
  33. [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 20));
  35. make.left.right.equalTo(self);
  36. make.bottom.equalTo(self).offset(-(55+45));
  37. }];
  38. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.left.top.right.equalTo(self);
  40. make.height.mas_equalTo(44);
  41. }];
  42. NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
  43. [dict setValue:@"app" forKey:@"ctl"];
  44. [dict setValue:@"room_memes_type_list" forKey:@"act"];
  45. [dict setValue:@"0" forKey:@"i_type"];
  46. [[NetHttpsManager manager] POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  47. if ([responseJson toInt:@"status"] == 1) {
  48. int i = 0;
  49. NSMutableArray *array = [NSMutableArray array];
  50. [array addObjectsFromArray:responseJson[@"list"]];
  51. // [array addObjectsFromArray:responseJson[@"list"]];
  52. // [array addObjectsFromArray:responseJson[@"list"]];
  53. for (NSDictionary *dict in array) {
  54. RoomFaceModel *model = [RoomFaceModel mj_objectWithKeyValues:dict];
  55. UIButton* Button=[[UIButton alloc]init];
  56. Button.imageEdgeInsets = UIEdgeInsetsMake(3, 10, 3, 10);
  57. Button.imageView.contentMode = UIViewContentModeScaleAspectFit;
  58. [Button sd_setImageWithURL:[NSURL URLWithString:SafeStr(model.img)] forState:UIControlStateNormal];
  59. [Button setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
  60. [Button setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"#3B3B5E"]] forState:UIControlStateSelected];
  61. [Button qmui_bindObject:model.id forKey:@"id"];
  62. [Button addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  63. Button.tag = i;
  64. ViewRadius(Button, 6);
  65. [self.bgView addSubview:Button];
  66. if (i == 0) {
  67. Button.selected = YES;
  68. self.selectedBtn = Button;
  69. [self requestDataWithID:model.id];
  70. }
  71. [Button mas_makeConstraints:^(MASConstraintMaker *make) {
  72. make.left.mas_equalTo(60*i);
  73. make.centerY.mas_equalTo(0);
  74. make.width.mas_equalTo(48);
  75. make.height.mas_equalTo(35);
  76. }];
  77. i++;
  78. }
  79. }
  80. } FailureBlock:^(NSError *error) {
  81. }];
  82. }
  83. return self;
  84. }
  85. - (void)ButtonClick:(UIButton *)sender {
  86. if (sender == self.selectedBtn) {
  87. return;
  88. }
  89. self.selectedBtn.selected = NO;
  90. sender.selected = YES;
  91. self.selectedBtn = sender;
  92. NSString *id = [sender qmui_getBoundObjectForKey:@"id"];
  93. [self requestDataWithID:id];
  94. }
  95. - (void)requestDataWithID:(NSString *)memes_type_id{
  96. NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
  97. [dict setValue:@"app" forKey:@"ctl"];
  98. [dict setValue:@"room_memes_list" forKey:@"act"];
  99. [dict setValue:@"0" forKey:@"i_type"];
  100. [dict setValue:memes_type_id forKey:@"memes_type_id"];
  101. [[NetHttpsManager manager] POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  102. if ([responseJson toInt:@"status"] == 1) {
  103. [self.dataArray removeAllObjects];
  104. for (NSDictionary *dict in responseJson[@"list"]) {
  105. RoomFaceModel *model = [RoomFaceModel mj_objectWithKeyValues:dict];
  106. [self.dataArray addObject:model];
  107. }
  108. }
  109. [self.collectionView reloadData];
  110. self.pageControl.numberOfPages = self.dataArray.count / 8 + (self.dataArray.count % 8 == 0 ? 0 : 1);
  111. } FailureBlock:^(NSError *error) {
  112. }];
  113. }
  114. - (void)show:(UIView *)superView{
  115. [super show:superView];
  116. }
  117. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  118. CGFloat contentOffsetX = scrollView.contentOffset.x;
  119. self.pageControl.currentPage = contentOffsetX / kScreenW;
  120. }
  121. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  122. return self.dataArray.count;
  123. }
  124. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  125. RoomFaceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([RoomFaceCell class]) forIndexPath:indexPath];
  126. [cell setModel:self.dataArray[indexPath.item]];
  127. return cell;
  128. }
  129. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  130. return CGSizeMake(floor(kScreenW / 4), 90);
  131. }
  132. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  133. if (self.delegate && [self.delegate respondsToSelector:@selector(faceView:didSelectFace:)]) {
  134. [self.delegate faceView:self didSelectFace:self.dataArray[indexPath.item]];
  135. }
  136. }
  137. - (NSMutableArray *)dataArray{
  138. if (!_dataArray) {
  139. _dataArray = [NSMutableArray array];
  140. }
  141. return _dataArray;
  142. }
  143. - (UIPageControl *)pageControl{
  144. if (!_pageControl) {
  145. _pageControl = [[UIPageControl alloc]initWithFrame:CGRectZero];
  146. _pageControl.currentPage = 0;
  147. }
  148. return _pageControl;
  149. }
  150. - (UICollectionView *)collectionView{
  151. if (!_collectionView) {
  152. HorizontalLayout *layout = [[HorizontalLayout alloc] init];
  153. layout.rowCount = 4;
  154. layout.columCount = 2;
  155. layout.itemSize = CGSizeMake(floor(kScreenW / 4), 90);
  156. layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
  157. layout.minimumLineSpacing = 0;
  158. layout.minimumInteritemSpacing = 0;
  159. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
  160. _collectionView.delegate = self;
  161. _collectionView.dataSource = self;
  162. _collectionView.pagingEnabled = YES;
  163. _collectionView.showsHorizontalScrollIndicator = NO;
  164. [_collectionView registerClass:[RoomFaceCell class] forCellWithReuseIdentifier:NSStringFromClass([RoomFaceCell class])];
  165. _collectionView.backgroundColor = kClearColor;
  166. }
  167. return _collectionView;
  168. }
  169. - (UIView *)bgView{
  170. if (!_bgView) {
  171. _bgView = [[UIView alloc]init];
  172. _bgView.backgroundColor = kClearColor;
  173. }
  174. return _bgView;
  175. }
  176. @end