ToolsView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. //
  2. // ToolsView.m
  3. // BuguLive
  4. //
  5. // Created by yy on 16/12/13.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "ToolsView.h"
  9. @implementation ToolsView
  10. {
  11. NSMutableArray *_dataArray;
  12. }
  13. static NSString *const cellId = @"cellId";
  14. - (instancetype)initWithFrame:(CGRect)frame
  15. {
  16. if (self = [super initWithFrame:frame])
  17. {
  18. _BuguLive = [GlobalVariables sharedInstance];
  19. if (!_toolsSelArray)
  20. {
  21. if ([GlobalVariables sharedInstance].isOtherPush) {
  22. _toolsSelArray = [[NSMutableArray alloc]initWithObjects:ASLocalizedString(@"外设直播_推流地址"), nil];
  23. _toolsNameArray = [[NSMutableArray alloc]initWithObjects:ASLocalizedString(@"推流地址"), nil];
  24. _toolsUnselArray = [[NSMutableArray alloc]initWithObjects:ASLocalizedString(@"外设直播_推流地址"), nil];
  25. }else{
  26. _toolsSelArray = [[NSMutableArray alloc]initWithObjects:
  27. @"lr_plugin_beauty_sel",
  28. @"lr_plugin_micro_sel",
  29. @"lr_plugin_camera_sel",
  30. @"lr_plugin_flash_sel",
  31. @"lr_plugin_mirror_sel",
  32. @"lr_plugin_sound_sel", nil];
  33. _toolsNameArray = [[NSMutableArray alloc]initWithObjects:
  34. ASLocalizedString(@"美颜"),
  35. ASLocalizedString(@"麦克风"),
  36. ASLocalizedString(@"翻转"),
  37. ASLocalizedString(@"闪光灯"),
  38. ASLocalizedString(@"镜像"), nil];
  39. _toolsUnselArray = [[NSMutableArray alloc]initWithObjects:
  40. @"lr_plugin_beauty_sel",
  41. @"lr_plugin_micro_unsel",
  42. @"lr_plugin_camera_unsel",
  43. @"lr_plugin_flash_unsel",
  44. @"lr_plugin_mirror_unsel", nil];
  45. }
  46. _cellArray = [[NSMutableArray alloc]init];
  47. _dataArray = [[NSMutableArray alloc]init];
  48. }
  49. [self setupModel];
  50. [self createToolCollection];
  51. }
  52. return self;
  53. }
  54. - (void)setupModel
  55. {
  56. dispatch_queue_t queue = dispatch_queue_create("toolsQueue", DISPATCH_QUEUE_SERIAL);
  57. dispatch_async(queue, ^{
  58. for (int i = 0; i < _toolsNameArray.count; i++)
  59. {
  60. PluginToolsModel *model = [[PluginToolsModel alloc]init];
  61. //2.麦克风
  62. if (i == 1)
  63. {
  64. model.isSelected = YES;
  65. }
  66. else
  67. {
  68. model.isSelected = NO;
  69. }
  70. [_dataArray addObject:model];
  71. }
  72. });
  73. }
  74. - (void)createToolCollection
  75. {
  76. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
  77. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  78. layout.itemSize = CGSizeMake(kScreenW / 4 - 20, kScreenW / 4 - 25);
  79. // CGSizeMake((self.size.width - 20 - 20 * 5) / 4 - 1, (self.size.width - 20 - 20 * 5) / 4 - 1);
  80. // layout.sectionInset = UIEdgeInsetsMake(27, 4, 0, 0);
  81. // layout.minimumLineSpacing = 0;
  82. // layout.minimumInteritemSpacing = 0;
  83. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  84. _toolsCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(10, 10, kScreenW - 20, self.size.height) collectionViewLayout:layout];
  85. _toolsCollectionView.backgroundColor = [UIColor clearColor];
  86. _toolsCollectionView.delegate = self;
  87. _toolsCollectionView.dataSource = self;
  88. _toolsCollectionView.showsHorizontalScrollIndicator = NO; //关闭滚动线
  89. _toolsCollectionView.allowsMultipleSelection = YES; //允许多选
  90. _toolsCollectionView.alwaysBounceHorizontal = YES; //总是允许横向滚动
  91. _toolsCollectionView.pagingEnabled = NO;
  92. [_toolsCollectionView registerClass:[ToolsCollectionViewCell class] forCellWithReuseIdentifier:cellId];
  93. [self addSubview:_toolsCollectionView];
  94. }
  95. #pragma mark --------------------------collectionView代理方法--------------------------
  96. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  97. {
  98. return 1;
  99. }
  100. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  101. {
  102. //如果是后置摄像头,则没有镜像
  103. if (![GlobalVariables sharedInstance].isOtherPush) {
  104. if (_isRearCamera)
  105. {
  106. if ([_toolsNameArray containsObject:ASLocalizedString(@"镜像")])
  107. {
  108. [_toolsNameArray removeObject:ASLocalizedString(@"镜像")];
  109. }
  110. }
  111. else
  112. {
  113. if (![_toolsNameArray containsObject:ASLocalizedString(@"镜像")])
  114. {
  115. [_toolsNameArray insertObject:ASLocalizedString(@"镜像")atIndex:4];
  116. }
  117. }
  118. }
  119. return _toolsNameArray.count;
  120. }
  121. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  122. {
  123. PluginToolsModel *model;
  124. if (indexPath.row < _dataArray.count)
  125. {
  126. model = _dataArray[indexPath.row];
  127. }
  128. ToolsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
  129. cell.toolLabel.text = _toolsNameArray[indexPath.row];
  130. [cell.toolBtn setTitle:_toolsNameArray[indexPath.row] forState:UIControlStateNormal];
  131. if (model.isSelected)
  132. {
  133. // cell.toolImgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[_toolsSelArray objectAtIndex:indexPath.row]]];
  134. [cell.toolBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",[_toolsSelArray objectAtIndex:indexPath.row]]] forState:UIControlStateNormal];
  135. }
  136. else
  137. {
  138. [cell.toolBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",[_toolsUnselArray objectAtIndex:indexPath.row]]] forState:UIControlStateNormal];
  139. // cell.toolImgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[_toolsUnselArray objectAtIndex:indexPath.row]]];
  140. }
  141. [_cellArray addObject:cell];
  142. return cell;
  143. }
  144. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  145. {
  146. return UIEdgeInsetsMake(0, 0, 0, 0);
  147. }
  148. #pragma mark 选中单元格
  149. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  150. {
  151. ToolsCollectionViewCell *cell = (ToolsCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  152. [self collectionView:_toolsCollectionView didHighlightItemAtIndexPath:indexPath];
  153. if (!SUS_WINDOW.isPushStreamIng && ![GlobalVariables sharedInstance].isOtherPush)
  154. {
  155. [self closePlugin];
  156. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"直播开启中,请稍候!")];
  157. [_toolsCollectionView deselectItemAtIndexPath:indexPath animated:NO];
  158. }
  159. else
  160. {
  161. [self judgmentTools:YES indexPath:indexPath cell:cell];
  162. }
  163. }
  164. #pragma mark 取消选中单元格
  165. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
  166. {
  167. ToolsCollectionViewCell *cell = (ToolsCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  168. [self judgmentTools:NO indexPath:indexPath cell:cell];
  169. }
  170. - (void)judgmentTools:(BOOL)selected indexPath:(NSIndexPath *)indexPath cell:(ToolsCollectionViewCell *)cell
  171. {
  172. PluginToolsModel *model;
  173. PluginToolsModel *mirrorModel; //镜像model
  174. PluginToolsModel *lightModel; //闪光灯model
  175. if (indexPath.row < _dataArray.count)
  176. {
  177. model = _dataArray[indexPath.row];
  178. }
  179. //0.音乐 1.美颜 3.切换摄像
  180. if (indexPath.row == 0 || indexPath.row == 2)
  181. {
  182. if (indexPath.row == 2)
  183. {
  184. _isRearCamera = !_isRearCamera;
  185. }
  186. // if(indexPath.row == 1)
  187. // {
  188. // model.isSelected = !model.isSelected;
  189. // }
  190. [self selectOrDeselect:indexPath.row isSelected:_isRearCamera];
  191. }
  192. //2.麦克风
  193. else if (indexPath.row == 1)
  194. {
  195. model.isSelected = !model.isSelected;
  196. [self selectOrDeselect:indexPath.row isSelected:model.isSelected];
  197. }
  198. //4.闪光灯
  199. else if (indexPath.row == 3)
  200. {
  201. //如果是后置摄像头
  202. if (_isRearCamera)
  203. {
  204. _isClick = !_isClick;
  205. }
  206. else
  207. {
  208. _isClick = NO;
  209. }
  210. model.isSelected = _isClick;
  211. [self selectOrDeselect:indexPath.row isSelected:_isClick];
  212. }
  213. //5.镜像
  214. else if (indexPath.row == 4)
  215. {
  216. // _isOpenMirror = !_isOpenMirror;
  217. model.isSelected = !model.isSelected;
  218. [self selectOrDeselect:indexPath.row isSelected:model.isSelected];
  219. }
  220. [_cellArray removeAllObjects];
  221. [_toolsCollectionView reloadData];
  222. }
  223. - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
  224. {
  225. return YES;
  226. }
  227. - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
  228. {
  229. PluginToolsModel *model;
  230. if (indexPath.row < _dataArray.count)
  231. {
  232. model = _dataArray[indexPath.row];
  233. }
  234. ToolsCollectionViewCell *cell = (ToolsCollectionViewCell *)[_toolsCollectionView cellForItemAtIndexPath:indexPath];
  235. if (model.isSelected)
  236. {
  237. cell.toolImgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[_toolsUnselArray objectAtIndex:indexPath.row]]];
  238. }
  239. else
  240. {
  241. cell.toolImgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[_toolsSelArray objectAtIndex:indexPath.row]]];
  242. }
  243. }
  244. - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
  245. {
  246. PluginToolsModel *model;
  247. if (indexPath.row < _dataArray.count)
  248. {
  249. model = _dataArray[indexPath.row];
  250. }
  251. ToolsCollectionViewCell *cell = (ToolsCollectionViewCell *)[_toolsCollectionView cellForItemAtIndexPath:indexPath];
  252. if (model.isSelected)
  253. {
  254. cell.toolImgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[_toolsSelArray objectAtIndex:indexPath.row]]];
  255. }
  256. else
  257. {
  258. cell.toolImgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[_toolsUnselArray objectAtIndex:indexPath.row]]];
  259. }
  260. }
  261. //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  262. // return 20;
  263. //}
  264. //
  265. //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
  266. // return 20;
  267. //}
  268. #pragma mark 取消闪光灯高亮图片
  269. - (void)closeLight:(NSIndexPath *)indexPath
  270. {
  271. ToolsCollectionViewCell *cell = [_cellArray objectAtIndex:indexPath.row +1];
  272. cell.toolImgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[_toolsUnselArray objectAtIndex:indexPath.row +1]]];
  273. }
  274. #pragma mark 关闭插件中心
  275. - (void)closePlugin
  276. {
  277. if (_toPCVdelegate && [_toPCVdelegate respondsToSelector:@selector(closeSelfView:)])
  278. {
  279. [_toPCVdelegate closeSelfView:self];
  280. }
  281. }
  282. - (void)selectOrDeselect:(NSInteger)row isSelected:(BOOL)isSelected
  283. {
  284. [self closePlugin];
  285. if (_toSDKdelegate && [_toSDKdelegate respondsToSelector:@selector(selectToolsItemWith:selectIndex:isSelected:)])
  286. {
  287. [_toSDKdelegate selectToolsItemWith:self selectIndex:row isSelected:isSelected];
  288. }
  289. }
  290. @end