MHSectionStickersView.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // MHSectionStickersView.m
  3. #import "MHSectionStickersView.h"
  4. #import "MHStickerCell.h"
  5. //#import "StickerManager.h"
  6. //#import "StickerDataListModel.h"
  7. #import "MHBeautyParams.h"
  8. #import <MHBeautySDK/MHBeautySDK.h>
  9. @interface MHSectionStickersView ()<UICollectionViewDelegate,UICollectionViewDataSource>
  10. @property (nonatomic, strong) NSMutableArray *indexsArr;
  11. @property (nonatomic, assign) NSInteger selectedIndex;
  12. @end
  13. @implementation MHSectionStickersView
  14. - (instancetype)initWithFrame:(CGRect)frame {
  15. if (self = [super initWithFrame:frame]) {
  16. [self addSubview:self.collectionView];
  17. self.lastIndex = -1;
  18. }
  19. return self;
  20. }
  21. - (void)configureData:(NSArray *)stickersArray {
  22. //NSLog(@"%@",stickersArray);
  23. [self.stickersArray removeAllObjects];
  24. [self.stickersArray addObjectsFromArray:stickersArray];
  25. [self.stickersArray removeObjectAtIndex:0];
  26. [self.collectionView reloadData];
  27. }
  28. - (void)configureStickerData:(NSInteger *)stickersType {
  29. }
  30. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  31. return self.stickersArray.count + (isNeedBottom?5:0);
  32. }
  33. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  34. MHStickerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MHStickerCell" forIndexPath:indexPath];
  35. if (self.stickersArray.count > indexPath.row) {
  36. cell.listModel = self.stickersArray[indexPath.row];
  37. }else{
  38. UICollectionViewCell * cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"customCollectionCell" forIndexPath:indexPath];
  39. return cell1;
  40. }
  41. return cell;
  42. }
  43. - (CGSize)collectionView:(UICollectionView *)collectionView
  44. layout:(UICollectionViewLayout *)collectionViewLayout
  45. sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  46. return CGSizeMake(MHStickerItemWidth, MHStickerItemHeight);
  47. }
  48. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  49. if (indexPath.row>=self.stickersArray.count) {
  50. return;
  51. }
  52. if (self.tag != self.lastTag) {//已经切换分类,取消上一个分类下的选中效果
  53. if ([self.delegate respondsToSelector:@selector(reloadLastStickerSelectedStatus:)]) {
  54. [self.delegate reloadLastStickerSelectedStatus:YES];
  55. }
  56. self.lastIndex = -1;
  57. }
  58. if (self.lastIndex == indexPath.item) {
  59. return;
  60. }
  61. StickerDataListModel *currentModel = self.stickersArray[indexPath.row];
  62. if (self.lastIndex >= 0) {
  63. StickerDataListModel *lastModel = self.stickersArray[self.lastIndex];
  64. lastModel.isSelected = NO;
  65. }
  66. MHStickerCell *cell = (MHStickerCell *)[collectionView cellForItemAtIndexPath:indexPath];
  67. [self.indexsArr removeAllObjects];//点击多个同时下载只显示最后一个贴纸特效
  68. [self.indexsArr addObject:@(indexPath.row)];
  69. if (currentModel.is_downloaded.boolValue == NO) {
  70. [cell startDownload];
  71. [[StickerManager sharedManager] downloadSticker:currentModel index:indexPath.row withSuccessed:^(StickerDataListModel * _Nonnull sticker, NSInteger index) {
  72. sticker.downloadState = MHStickerDownloadStateDownoadDone;
  73. [self.stickersArray replaceObjectAtIndex:indexPath.item withObject:sticker];
  74. NSNumber *lastSelectedIndex = self.indexsArr.lastObject;
  75. if (index == lastSelectedIndex.integerValue) {
  76. sticker.isSelected = YES;
  77. NSString *key = [NSString stringWithFormat:@"%@:%@",sticker.name,sticker.uptime];
  78. if ([self.delegate respondsToSelector:@selector(handleSelectedStickerEffect: stickerModel:)]) {
  79. [self.delegate handleSelectedStickerEffect:key stickerModel:sticker];
  80. }
  81. }
  82. dispatch_async(dispatch_get_main_queue(), ^{
  83. if (collectionView) {
  84. for (NSIndexPath *path in collectionView.indexPathsForVisibleItems) {
  85. if (index == path.item) {
  86. [collectionView reloadData];
  87. break;
  88. }
  89. }
  90. }
  91. });
  92. self.lastIndex = indexPath.item;
  93. NSString *itemStr = [NSString stringWithFormat:@"%ld",(long)indexPath.item];
  94. [[NSUserDefaults standardUserDefaults] setObject:itemStr forKey:@"selectedStickerIndex"];
  95. } failed:^(StickerDataListModel * _Nonnull sticker, NSInteger index) {
  96. sticker.isSelected = NO;
  97. sticker.downloadState = MHStickerDownloadStateDownoadNot;
  98. [self.stickersArray replaceObjectAtIndex:indexPath.item withObject:sticker];
  99. if (self.lastIndex >= 0) {
  100. StickerDataListModel *lastModel = self.stickersArray[self.lastIndex];
  101. lastModel.isSelected = YES;
  102. }
  103. dispatch_async(dispatch_get_main_queue(), ^{
  104. [collectionView reloadData];
  105. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"下载失败" message:@"请稍后重试" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
  106. [alert show];
  107. });
  108. }];
  109. } else {
  110. // if (self.lastTag == 1114) {
  111. for (StickerDataListModel * model in self.stickersArray) {
  112. if (model == currentModel) {
  113. model.isSelected = YES;
  114. }else{
  115. model.isSelected = NO;
  116. }
  117. }
  118. // }
  119. NSString *key = [NSString stringWithFormat:@"%@:%@",currentModel.name,currentModel.uptime];
  120. if ([self.delegate respondsToSelector:@selector(handleSelectedStickerEffect: stickerModel:)]) {
  121. [self.delegate handleSelectedStickerEffect:key stickerModel:currentModel];
  122. }
  123. [collectionView reloadData];
  124. self.lastIndex = indexPath.item;
  125. NSString *itemStr = [NSString stringWithFormat:@"%ld",(long)indexPath.item];
  126. [[NSUserDefaults standardUserDefaults] setObject:itemStr forKey:@"selectedStickerIndex"];
  127. }
  128. }
  129. #pragma mark - lazy
  130. - (NSMutableArray *)indexsArr {
  131. if (!_indexsArr) {
  132. _indexsArr = [NSMutableArray array];
  133. }
  134. return _indexsArr;
  135. }
  136. -(NSMutableArray *)stickersArray {
  137. if (!_stickersArray) {
  138. _stickersArray = [NSMutableArray array];
  139. }
  140. return _stickersArray;
  141. }
  142. - (UICollectionView *)collectionView {
  143. if (!_collectionView) {
  144. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  145. layout.minimumLineSpacing = 15;
  146. layout.minimumInteritemSpacing = 15;
  147. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  148. layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
  149. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, window_width, self.frame.size.height) collectionViewLayout:layout];
  150. ///修改MHUI
  151. _collectionView.backgroundColor = [UIColor clearColor];
  152. _collectionView.showsHorizontalScrollIndicator = NO;
  153. _collectionView.delegate = self;
  154. _collectionView.dataSource = self;
  155. [_collectionView registerClass:[MHStickerCell class] forCellWithReuseIdentifier:@"MHStickerCell"];
  156. [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"customCollectionCell"];
  157. }
  158. return _collectionView;
  159. }
  160. @end