MHMagnifiedView.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // MHMagnifiedView.m
  3. //哈哈镜
  4. #import "MHMagnifiedView.h"
  5. #import "MHBeautyMenuCell.h"
  6. #import "MHBeautiesModel.h"
  7. #import "MHBeautyParams.h"
  8. #import "WNSegmentControl.h"
  9. #import "MHBottomView.h"
  10. @interface MHMagnifiedView ()<UICollectionViewDelegate,UICollectionViewDataSource>
  11. @property (nonatomic, strong) UICollectionView *collectionView;
  12. @property (nonatomic, strong) NSMutableArray *array;
  13. @property (nonatomic, assign) NSInteger lastIndex;
  14. ///修改MHUI
  15. @property (nonatomic, strong) WNSegmentControl *segmentControl;
  16. @property (nonatomic, strong) UIView *lineView;
  17. ///修改MHUI
  18. @property (nonatomic, strong) MHBottomView * bottomView;
  19. @end
  20. @implementation MHMagnifiedView
  21. - (instancetype)initWithFrame:(CGRect)frame {
  22. if (self = [super initWithFrame:frame]) {
  23. ///修改MHUI
  24. // [self addSubview:self.segmentControl];
  25. // [self addSubview:self.lineView];
  26. [self addSubview:self.collectionView];
  27. ///修改MHUI
  28. if (isNeedBottom) {
  29. [self addSubview:self.bottomView];
  30. }
  31. }
  32. return self;
  33. }
  34. - (void)setIsHiddenHead:(BOOL)isHiddenHead{
  35. _isHiddenHead = isHiddenHead;
  36. _segmentControl.hidden = _isHiddenHead;
  37. if (_isHiddenHead) {
  38. [_lineView removeFromSuperview];
  39. [_bottomView removeFromSuperview];
  40. self.collectionView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
  41. }
  42. }
  43. #pragma mark - 底部按钮响应
  44. - (void)cameraAction:(BOOL)isTakePhoto{
  45. NSLog(@"点击了拍照");
  46. if (isTakePhoto) {
  47. if ([self.delegate respondsToSelector:@selector(takePhoto)]) {
  48. [self.delegate takePhoto];
  49. }
  50. }else{
  51. if ([self.delegate respondsToSelector:@selector(clickPackUp)]) {
  52. [self.delegate clickPackUp];
  53. }
  54. }
  55. }
  56. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  57. return self.array.count;
  58. }
  59. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  60. MHBeautyMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MHMagnifiedEffectCell" forIndexPath:indexPath];
  61. cell.hahaModel = self.array[indexPath.row];
  62. return cell;
  63. }
  64. - (CGSize)collectionView:(UICollectionView *)collectionView
  65. layout:(UICollectionViewLayout *)collectionViewLayout
  66. sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  67. return CGSizeMake((window_width-20)/4.5, 100);
  68. }
  69. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  70. if (self.lastIndex == indexPath.row) {
  71. return;
  72. }
  73. MHBeautiesModel *model = self.array[indexPath.row];
  74. model.isSelected = !model.isSelected;
  75. MHBeautiesModel *lastModel = self.array[self.lastIndex];
  76. lastModel.isSelected = !lastModel.isSelected;
  77. [self.collectionView reloadData];
  78. self.lastIndex = indexPath.row;
  79. if ([self.delegate respondsToSelector:@selector(handleMagnify:withIsMenu:)]) {
  80. [self.delegate handleMagnify:model.type withIsMenu:!_isHiddenHead];
  81. }
  82. if (isSaveHaHaMirror) {
  83. [[NSUserDefaults standardUserDefaults] setInteger:model.type forKey:kMHHaHaMirror];
  84. }
  85. }
  86. #pragma mark - lazy
  87. -(NSMutableArray *)array {
  88. if (!_array) {
  89. NSMutableArray * selectedItemArray = [NSMutableArray array];
  90. if (_isHiddenHead) {
  91. selectedItemArray = [MHSDK shareInstance].magnifiedArray;
  92. }else{
  93. selectedItemArray = [MHSDK shareInstance].meunMagnifiedArray;
  94. }
  95. NSString *path = [[NSBundle mainBundle] pathForResource:@"MHMagnifiedEffectParams" ofType:@"plist"];
  96. NSArray *items = [NSArray arrayWithContentsOfFile:path];
  97. NSMutableArray * selectedItems = [NSMutableArray array];
  98. for (int i = 0; i < selectedItemArray.count; i ++) {
  99. NSDictionary * selectedItemDic = selectedItemArray[i];
  100. NSString * selectedName = selectedItemDic[@"name"];
  101. for (int j = 0; j < items.count; j++) {
  102. NSDictionary * itemDic = items[j];
  103. NSString * itemName = itemDic[@"name"];
  104. if ([selectedName isEqual:itemName]) {
  105. [selectedItems addObject:itemDic];
  106. }
  107. }
  108. }
  109. _array = [NSMutableArray array];
  110. for (int i = 0; i<selectedItems.count; i++) {
  111. NSDictionary * itemDic = selectedItems[i];
  112. MHBeautiesModel *model = [[MHBeautiesModel alloc] init];
  113. model.imgName = itemDic[@"imageName"];
  114. model.beautyTitle = itemDic[@"name"];
  115. model.type = [itemDic[@"type"] integerValue];
  116. model.menuType = MHBeautyMenuType_Magnify;
  117. if (isSaveHaHaMirror) {
  118. NSNumber *type = [[NSUserDefaults standardUserDefaults] objectForKey:kMHHaHaMirror];
  119. if (model.type == type.integerValue) {
  120. model.isSelected = YES;
  121. self.lastIndex = i;
  122. }else{
  123. model.isSelected = NO;
  124. }
  125. }else{
  126. model.isSelected = i == 0 ? YES : NO;
  127. }
  128. [_array addObject:model];
  129. }
  130. }
  131. return _array;
  132. }
  133. - (UICollectionView *)collectionView {
  134. if (!_collectionView) {
  135. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  136. layout.minimumLineSpacing = 0;
  137. layout.minimumInteritemSpacing = 10;
  138. layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
  139. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  140. ///修改MHUI
  141. CGFloat bottom = _lineView.frame.origin.y + _lineView.frame.size.height;
  142. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, bottom, window_width, self.frame.size.height - bottom - MHBottomViewHeight) collectionViewLayout:layout];
  143. ///修改MHUI
  144. _collectionView.backgroundColor = [UIColor clearColor];
  145. _collectionView.delegate = self;
  146. _collectionView.dataSource = self;
  147. [_collectionView registerClass:[MHBeautyMenuCell class] forCellWithReuseIdentifier:@"MHMagnifiedEffectCell"];
  148. _collectionView.showsHorizontalScrollIndicator = NO;
  149. }
  150. return _collectionView;
  151. }
  152. ///修改MHUI
  153. - (WNSegmentControl *)segmentControl {
  154. if (!_segmentControl) {
  155. _segmentControl = [[WNSegmentControl alloc] initWithTitles:@[@"哈哈镜"]];
  156. _segmentControl.frame = CGRectMake(0, 0, window_width, MHStickerSectionHeight);
  157. _segmentControl.backgroundColor = [UIColor clearColor];
  158. [_segmentControl setTextAttributes:@{NSFontAttributeName: Font_12, NSForegroundColorAttributeName: FontColorNormal}
  159. forState:UIControlStateNormal];
  160. [_segmentControl setTextAttributes:@{NSFontAttributeName: Font_12, NSForegroundColorAttributeName: FontColorSelected}
  161. forState:UIControlStateSelected];
  162. _segmentControl.selectedSegmentIndex = 0;
  163. _segmentControl.widthStyle = WNSegmentedControlWidthStyleFixed;
  164. // [_segmentControl addTarget:self action:@selector(switchList:) forControlEvents:UIControlEventValueChanged];
  165. }
  166. return _segmentControl;
  167. }
  168. - (UIView *)lineView {
  169. if (!_lineView) {
  170. CGFloat bottom = _segmentControl.frame.origin.y + _segmentControl.frame.size.height;
  171. _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, bottom, window_width, 0.5)];
  172. _lineView.backgroundColor = LineColor;
  173. }
  174. return _lineView;
  175. }
  176. ///修改MHUI
  177. - (MHBottomView*)bottomView{
  178. if (!_bottomView) {
  179. __weak typeof(self) weakSelf = self;
  180. CGFloat bottom = self.collectionView.frame.origin.y + self.collectionView.frame.size.height;
  181. _bottomView = [[MHBottomView alloc] initWithFrame:CGRectMake(0, bottom, window_width, MHBottomViewHeight)];
  182. // _bottomView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
  183. _bottomView.clickBtn = ^(BOOL isTakePhoto) {
  184. [weakSelf cameraAction:isTakePhoto];
  185. };
  186. }
  187. return _bottomView;
  188. }
  189. @end