BogoOpenWardCollectionView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //
  2. // BogoOpenWardCollectionView.m
  3. // BuguLive
  4. //
  5. // Created by 宋晨光 on 2021/10/8.
  6. // Copyright © 2021 xfg. All rights reserved.
  7. //
  8. #import "BogoOpenWardCollectionView.h"
  9. #import "BogoOpenWardCollectionCell.h"
  10. @interface BogoOpenWardCollectionView()
  11. @property(nonatomic, assign) NSInteger selectRow;
  12. @end
  13. @implementation BogoOpenWardCollectionView
  14. - (instancetype)initWithFrame:(CGRect)frame
  15. {
  16. self = [super initWithFrame:frame];
  17. if (self) {
  18. self.selectRow = 0;
  19. [self setUpView];
  20. }
  21. return self;
  22. }
  23. -(void)setUpView{
  24. self.backgroundColor = kClearColor;
  25. self.collectionView.scrollEnabled = NO;
  26. [self addSubview:self.collectionView];
  27. }
  28. -(void)refreshType:(BOGO_OPENWARD_Collection_TYPE)type array:(NSArray *)dataArray{
  29. _type = type;
  30. _dataArray = [NSMutableArray arrayWithArray:dataArray];
  31. self.selectRow = 0;
  32. [self.collectionView reloadData];
  33. }
  34. - (void)refreshTypeNameWithArray:(NSArray *)dataArray{
  35. self.selectRow = 0;
  36. for (int i = 0; i < self.dataArray.count ; i++) {
  37. BogoWardModel *model = self.dataArray[i];
  38. model.isSelect = NO;
  39. if (dataArray.count > i) {
  40. if ([dataArray[i] isEqualToString:model.type]) {
  41. model.isSelect = YES;
  42. }
  43. }
  44. [self.dataArray replaceObjectAtIndex:i withObject:model];
  45. }
  46. [self.collectionView reloadData];
  47. }
  48. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  49. return 1;
  50. }
  51. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  52. if (self.dataArray) {
  53. return _dataArray.count;
  54. }
  55. return 1;
  56. }
  57. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  58. BogoOpenWardCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BogoOpenWardCollectionCell" forIndexPath:indexPath];
  59. BogoWardModel *model = self.dataArray[indexPath.row];
  60. if ([model isKindOfClass:[NSDictionary class]]) {
  61. model = [BogoWardModel modelWithDictionary:self.dataArray[indexPath.row]];
  62. }
  63. [cell.btn setTitle:model.name forState:UIControlStateNormal];
  64. if (self.type == BOGO_OPENWARD_Collection_TYPE_GUARDIANS) {
  65. cell.btn.spacingBetweenImageAndTitle = 10;
  66. cell.btn.imagePosition = QMUIButtonImagePositionTop;
  67. [cell.btn setTitle:[NSString stringWithFormat:ASLocalizedString(@"%@守护"),model.name] forState:UIControlStateNormal];
  68. // [cell.btn setImageWithURL:[NSURL URLWithString:model.img] forState:UIControlStateNormal options:YYWebImageOptionShowNetworkActivity];
  69. WeakSelf
  70. [cell.btn sd_setImageWithURL:[NSURL URLWithString:model.img] forState:UIControlStateNormal completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  71. image = [weakSelf scaleImage:image scaleToSize:CGSizeMake(kRealValue(50), kRealValue(18))];
  72. [cell.btn setImage:image forState:UIControlStateNormal];
  73. }];
  74. cell.btn.layer.cornerRadius = 5;
  75. cell.btn.layer.masksToBounds = YES;
  76. if (self.selectRow == indexPath.row) {
  77. [cell.btn setTitleColor:[UIColor colorWithHexString:@"#9152F8"] forState:UIControlStateNormal];
  78. cell.btn.backgroundColor = [UIColor colorWithHexString:@"#F6EDFF"];
  79. cell.btn.layer.borderWidth = 1.5f;
  80. cell.btn.layer.borderColor = [UIColor colorWithHexString:@"#9152F8"].CGColor;
  81. }else{
  82. [cell.btn setTitleColor:[UIColor colorWithHexString:@"#777777"] forState:UIControlStateNormal];
  83. cell.btn.backgroundColor = [UIColor colorWithHexString:@"#F4F4F4"];
  84. cell.btn.layer.borderWidth = 0;
  85. }
  86. }else if(self.type == BOGO_OPENWARD_Collection_TYPE_PRIVITE){
  87. cell.btn.layer.borderWidth = 0;
  88. cell.btn.spacingBetweenImageAndTitle = 5;
  89. cell.btn.imagePosition = QMUIButtonImagePositionTop;
  90. cell.btn.backgroundColor = kClearColor;
  91. cell.btn.titleLabel.font = [UIFont systemFontOfSize:12];
  92. if (model.isSelect) {
  93. // [cell.btn setImageWithURL:[NSURL URLWithString:model.select_icon] forState:UIControlStateNormal options:YYWebImageOptionShowNetworkActivity];
  94. WeakSelf
  95. [cell.btn sd_setImageWithURL:[NSURL URLWithString:model.select_icon] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  96. if(!error)
  97. {
  98. image = [weakSelf scaleImage:image scaleToSize:CGSizeMake(kRealValue(40), kRealValue(40))];
  99. [cell.btn setImage:image forState:UIControlStateNormal];
  100. }
  101. else
  102. {
  103. image = [weakSelf scaleImage:[UIImage imageNamed:@"DefaultImg"] scaleToSize:CGSizeMake(kRealValue(40), kRealValue(40))];
  104. [cell.btn setImage:image forState:UIControlStateNormal];
  105. }
  106. }];
  107. [cell.btn setTitleColor:[UIColor colorWithHexString:@"#AE2CF1"] forState:UIControlStateNormal];
  108. }else{
  109. // [cell.btn setImageWithURL:[NSURL URLWithString:model.default_icon] forState:UIControlStateNormal options:YYWebImageOptionShowNetworkActivity];
  110. WeakSelf
  111. [cell.btn sd_setImageWithURL:[NSURL URLWithString:model.default_icon] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  112. image = [weakSelf scaleImage:image scaleToSize:CGSizeMake(kRealValue(40), kRealValue(40))];
  113. [cell.btn setImage:image forState:UIControlStateNormal];
  114. if(!error)
  115. {
  116. image = [weakSelf scaleImage:image scaleToSize:CGSizeMake(kRealValue(40), kRealValue(40))];
  117. [cell.btn setImage:image forState:UIControlStateNormal];
  118. }
  119. else
  120. {
  121. image = [weakSelf scaleImage:[UIImage imageNamed:@"DefaultImg"] scaleToSize:CGSizeMake(kRealValue(40), kRealValue(40))];
  122. [cell.btn setImage:image forState:UIControlStateNormal];
  123. }
  124. }];
  125. [cell.btn setTitleColor:[UIColor colorWithHexString:@"#AE2CF1"] forState:UIControlStateNormal];
  126. }
  127. }else if(self.type == BOGO_OPENWARD_Collection_TYPE_TIME){
  128. cell.btn.layer.cornerRadius = 4;
  129. cell.btn.layer.masksToBounds = YES;
  130. cell.btn.spacingBetweenImageAndTitle = 5;
  131. cell.btn.imagePosition = QMUIButtonImagePositionLeft;
  132. BogoWardPayTimeModel *timeModel = self.dataArray[indexPath.row];
  133. if ([self.dataArray[indexPath.row] isKindOfClass:[NSDictionary class]]) {
  134. timeModel = [BogoWardPayTimeModel modelWithDictionary:self.dataArray[indexPath.row]];
  135. }
  136. NSString *timeStr = [NSString stringWithFormat:@"%@/%@",timeModel.coin,timeModel.name];
  137. [cell.btn setTitle:timeStr forState:UIControlStateNormal];
  138. [cell.btn setImage:[UIImage imageNamed:@"bogo_me_top_diamond"] forState:UIControlStateNormal];
  139. if (self.selectRow == indexPath.row) {
  140. [cell.btn setTitleColor:[UIColor colorWithHexString:@"#9152F8"] forState:UIControlStateNormal];
  141. cell.btn.backgroundColor = [UIColor colorWithHexString:@"#F6EDFF"];
  142. cell.btn.layer.borderWidth = 1.5f;
  143. cell.btn.layer.borderColor = [UIColor colorWithHexString:@"#9152F8"].CGColor;
  144. }else{
  145. [cell.btn setTitleColor:[UIColor colorWithHexString:@"#777777"] forState:UIControlStateNormal];
  146. cell.btn.backgroundColor = [UIColor colorWithHexString:@"#F4F4F4"];
  147. cell.btn.layer.borderWidth = 0;
  148. }
  149. }
  150. cell.btn.selected = model.isSelect;
  151. return cell;
  152. }
  153. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  154. if (self.type == BOGO_OPENWARD_Collection_TYPE_PRIVITE) {
  155. return;
  156. }
  157. self.selectRow = indexPath.row;
  158. //点击守护
  159. if (self.selectRowBlock && self.type == BOGO_OPENWARD_Collection_TYPE_GUARDIANS) {
  160. self.selectRowBlock(self.dataArray[indexPath.row]);
  161. }
  162. //点击时长
  163. if (self.selectTimeCollectionViewRowBlock && self.type == BOGO_OPENWARD_Collection_TYPE_TIME) {
  164. self.selectTimeCollectionViewRowBlock(self.dataArray[indexPath.row]);
  165. }
  166. [self.collectionView reloadData];
  167. }
  168. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  169. if (self.type == BOGO_OPENWARD_Collection_TYPE_GUARDIANS) {
  170. CGFloat cellWidth = ceil((self.collectionView.width/3 - 42/3.0)) ;
  171. return CGSizeMake(cellWidth, (78));
  172. }else if (self.type == BOGO_OPENWARD_Collection_TYPE_PRIVITE) {
  173. // CGFloat cellWidth = ceil((self.collectionView.width/4 - 30/3.0)) ;
  174. return CGSizeMake(ceil(self.collectionView.width/4 - 20), (78));
  175. // return CGSizeMake((self.width - (10 * 3 + 10 * 2)) / 4, (75));
  176. }
  177. return CGSizeMake((self.width - (12 * 2 + 10)) / 2, (40));
  178. }
  179. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  180. return 0;
  181. }
  182. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  183. {
  184. if (self.type == BOGO_OPENWARD_Collection_TYPE_GUARDIANS) {
  185. return UIEdgeInsetsMake(0, 10, 10, 10);
  186. }
  187. else if (self.type == BOGO_OPENWARD_Collection_TYPE_PRIVITE) {
  188. return UIEdgeInsetsMake(0, 0, 0, 10);
  189. }
  190. return UIEdgeInsetsMake(0, 10, 0, 10);
  191. }
  192. - (UICollectionView *)collectionView{
  193. if (!_collectionView) {
  194. UICollectionViewFlowLayout *contentLayout = [[UICollectionViewFlowLayout alloc] init];
  195. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:contentLayout];
  196. _collectionView.backgroundColor = kClearColor;
  197. _collectionView.delegate = self;
  198. _collectionView.dataSource = self;
  199. [_collectionView registerNib:[UINib nibWithNibName:@"BogoOpenWardCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"BogoOpenWardCollectionCell"];
  200. }
  201. return _collectionView;
  202. }
  203. - (UIImage*)scaleImage:(UIImage *)image scaleToSize:(CGSize)size{
  204. UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
  205. [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
  206. UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  207. UIGraphicsEndImageContext();
  208. return scaledImage;
  209. }
  210. @end