CommonToolMoveVC.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //
  2. // CommonToolMoveVC.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/4/29.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "CommonToolMoveVC.h"
  9. #import "WorkFLowCell.h"
  10. @interface CommonToolMoveVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  11. @property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
  12. @property (weak, nonatomic) IBOutlet UIButton *doneBtn;
  13. @property (strong,nonatomic) NSMutableArray *dataSourceArray;
  14. @property (nonatomic,assign) BOOL isBeganMove;
  15. @end
  16. @implementation CommonToolMoveVC
  17. +(CommonToolMoveVC *)initCommonToolMoveVC{
  18. CommonToolMoveVC *controller = [StoryboardManager.shared.Common instantiateViewControllerWithIdentifier:@"CommonToolMoveVC"];
  19. return controller;
  20. }
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. self.fd_prefersNavigationBarHidden = YES;
  24. [self.view setBackgroundColor:UIColorHex(0xf7f7f7)];
  25. [self initDataSourceInfo];
  26. [self.collectionView setCollectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
  27. [self.collectionView registerNib:[UINib nibWithNibName:@"WorkFLowCell" bundle:nil] forCellWithReuseIdentifier:@"WorkFLowCellID"];
  28. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReusableView"];
  29. self.collectionView.showsVerticalScrollIndicator = NO;
  30. self.collectionView.showsHorizontalScrollIndicator = NO;
  31. self.collectionView.alwaysBounceVertical = YES;
  32. [self.collectionView setDataSource:self];
  33. [self.collectionView setDelegate:self];
  34. [self.collectionView reloadData];
  35. WS(weakSelf);
  36. [self.doneBtn setAction:^{
  37. [weakSelf.navigationController popViewControllerAnimated:YES];
  38. }];
  39. }
  40. #pragma mark - 手势事件
  41. -(void)moveCollectionViewCell:(UILongPressGestureRecognizer *)gesture {
  42. switch (gesture.state) {
  43. case UIGestureRecognizerStateBegan: {
  44. if (!self.isBeganMove) {
  45. self.isBeganMove = YES;
  46. //获取点击的cell的indexPath
  47. NSIndexPath *selectedIndexPath = [self.collectionView indexPathForItemAtPoint:[gesture locationInView:self.collectionView]];
  48. //开始移动对应的cell
  49. [self.collectionView beginInteractiveMovementForItemAtIndexPath:selectedIndexPath];
  50. for (WorkFLowCell *cell in [self.collectionView visibleCells]) {
  51. [self starShake:cell];
  52. }
  53. }
  54. break;
  55. }
  56. case UIGestureRecognizerStateChanged: {
  57. //移动cell
  58. [self.collectionView updateInteractiveMovementTargetPosition:[gesture locationInView:self.collectionView]];
  59. break;
  60. }
  61. case UIGestureRecognizerStateEnded: {
  62. self.isBeganMove = NO;
  63. //结束移动
  64. [self.collectionView endInteractiveMovement];
  65. for (WorkFLowCell *cell in [self.collectionView visibleCells]) {
  66. [self stopShake:cell];
  67. }
  68. break;
  69. }
  70. default:
  71. [self.collectionView cancelInteractiveMovement];
  72. break;
  73. }
  74. }
  75. - (void)starShake:(WorkFLowCell*)cell{
  76. CAKeyframeAnimation * keyAnimaion = [CAKeyframeAnimation animation];
  77. keyAnimaion.keyPath = @"transform.rotation";
  78. keyAnimaion.values = @[@(-3 / 180.0 * M_PI),@(3 /180.0 * M_PI),@(-3/ 180.0 * M_PI)];//度数转弧度
  79. keyAnimaion.removedOnCompletion = NO;
  80. keyAnimaion.fillMode = kCAFillModeForwards;
  81. keyAnimaion.duration = 0.3;
  82. keyAnimaion.repeatCount = MAXFLOAT;
  83. [cell.layer addAnimation:keyAnimaion forKey:@"cellShake"];
  84. }
  85. - (void)stopShake:(WorkFLowCell*)cell{
  86. [cell.layer removeAnimationForKey:@"cellShake"];
  87. }
  88. /**********************************************************************/
  89. #pragma mark -构造WQRefreshCollectionView的数据源对象
  90. /**********************************************************************/
  91. -(void)initDataSourceInfo{
  92. NSDictionary * chatMenuDict = [[NSUserDefaults standardUserDefaults] objectForKey:CHATMENUBAR];
  93. for(int m=0;m<2;m++){
  94. MoreAppModel *bean = [[MoreAppModel alloc] init];
  95. switch (m) {
  96. case 0:{
  97. bean.isCloseSectionFlag = YES;
  98. NSArray * array = [chatMenuDict objectForKey:@"common"];
  99. NSMutableArray * muArray = [NSMutableArray array];
  100. for (NSInteger i = 0; i < 9; i ++) {
  101. MoreAppInfoModel * model = [MoreAppInfoModel modelWithDictionary:array.firstObject];
  102. [muArray addObject:model];
  103. }
  104. // for (NSDictionary * dict in array) {
  105. // MoreAppInfoModel * model = [MoreAppInfoModel modelWithDictionary:dict];
  106. // [muArray addObject:model];
  107. // }
  108. bean.moreAppInfoArray = muArray;
  109. }break;
  110. case 1:{
  111. NSArray * array = [chatMenuDict objectForKey:@"more"];
  112. NSMutableArray * muArray = [NSMutableArray array];
  113. for (NSInteger i = 0; i < 9; i ++) {
  114. MoreAppInfoModel * model = [MoreAppInfoModel modelWithDictionary:array.firstObject];
  115. [muArray addObject:model];
  116. }
  117. // for (NSDictionary * dict in array) {
  118. // MoreAppInfoModel * model = [MoreAppInfoModel modelWithDictionary:dict];
  119. // [muArray addObject:model];
  120. // }
  121. bean.moreAppInfoArray = muArray;
  122. }break;
  123. default:
  124. break;
  125. }
  126. [self.dataSourceArray addObject:bean];
  127. }
  128. }
  129. /**********************************************************************/
  130. #pragma mark -UICollectionViewDataSource
  131. /**********************************************************************/
  132. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  133. return self.dataSourceArray.count;
  134. }
  135. /**********************************************************************/
  136. #pragma mark -GHRefreshCollectionView的section的Count
  137. /**********************************************************************/
  138. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  139. MoreAppModel *bean = [self.dataSourceArray objectAtIndex:section];
  140. return bean.moreAppInfoArray.count;
  141. }
  142. #pragma mark - UICollectionViewDelegate
  143. - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
  144. MoreAppModel *bean = [self.dataSourceArray objectAtIndex:indexPath.section];
  145. MoreAppInfoModel *model = [bean.moreAppInfoArray objectAtIndex:indexPath.item];
  146. WorkFLowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WorkFLowCellID" forIndexPath:indexPath];
  147. UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(moveCollectionViewCell:)];
  148. cell.userInteractionEnabled = YES;
  149. [cell addGestureRecognizer:longPressGesture];
  150. cell.titleLabel.text = [NSString stringWithFormat:@"工具%ld",(indexPath.item + 1)];
  151. cell.iconImageView.image = IMG(@"img_placeHolder");
  152. cell.leftLine.hidden = NO;
  153. cell.rightLine.hidden = NO;
  154. cell.bottomLine.hidden= NO;
  155. cell.topLine.hidden = NO;
  156. cell.showImg.hidden = NO;
  157. if(indexPath.section == 0){
  158. [cell.showImg setImage:[UIImage imageNamed:@"chatmsg_edit_del_icon"]];
  159. }else{
  160. [cell.showImg setImage:[UIImage imageNamed:@"chatmsg_edit_add_icon"]];
  161. }
  162. return cell;
  163. }
  164. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  165. return CGSizeMake((kGXScreenWidth -14)/3 ,(kGXScreenWidth -14)/3);
  166. }
  167. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  168. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  169. // MoreAppModel *bean = [self.dataSourceArray objectAtIndex:indexPath.section];
  170. // MoreAppInfoModel *beanItem = [bean.moreAppInfoArray objectAtIndex:indexPath.item];
  171. // if(indexPath.section == 0){
  172. // if (indexPath.item == 0) {
  173. // return;
  174. // }
  175. // [bean.moreAppInfoArray removeObject:beanItem];
  176. // MoreAppModel *moreModel = [self.dataSourceArray lastObject];
  177. // [moreModel.moreAppInfoArray addObject:beanItem];
  178. // NSMutableArray * moreArray = [NSMutableArray array];
  179. // for (MoreAppInfoModel * model in moreModel.moreAppInfoArray) {
  180. // NSDictionary * dict = [model modelToJSONObject];
  181. // [moreArray addObject:dict];
  182. // }
  183. //
  184. // MoreAppModel *commonModel = [self.dataSourceArray firstObject];
  185. // NSMutableArray * commonArray = [NSMutableArray array];
  186. // for (MoreAppInfoModel * model in commonModel.moreAppInfoArray) {
  187. // NSDictionary * dict = [model modelToJSONObject];
  188. // [commonArray addObject:dict];
  189. // }
  190. // [[NSUserDefaults standardUserDefaults] setObject:@{@"common":commonArray,@"more":moreArray} forKey:CHATMENUBAR];
  191. // [[NSUserDefaults standardUserDefaults] synchronize];
  192. // [self.collectionView reloadData];
  193. // }else {
  194. // [bean.moreAppInfoArray removeObject:beanItem];
  195. // MoreAppModel *commonModel = [self.dataSourceArray firstObject];
  196. // [commonModel.moreAppInfoArray addObject:beanItem];
  197. // NSMutableArray * commonArray = [NSMutableArray array];
  198. // for (MoreAppInfoModel * model in commonModel.moreAppInfoArray) {
  199. // NSDictionary * dict = [model modelToJSONObject];
  200. // [commonArray addObject:dict];
  201. // }
  202. // MoreAppModel *moreModel = [self.dataSourceArray lastObject];
  203. // NSMutableArray * moreArray = [NSMutableArray array];
  204. // for (MoreAppInfoModel * model in moreModel.moreAppInfoArray) {
  205. // NSDictionary * dict = [model modelToJSONObject];
  206. // [moreArray addObject:dict];
  207. // }
  208. // [[NSUserDefaults standardUserDefaults] setObject:@{@"common":commonArray,@"more":moreArray} forKey:CHATMENUBAR];
  209. // [[NSUserDefaults standardUserDefaults] synchronize];
  210. // [self.collectionView reloadData];
  211. // }
  212. }
  213. //footer的size
  214. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  215. {
  216. return CGSizeMake(0, 0);
  217. }
  218. //header的size
  219. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  220. {
  221. if (section == 0) {
  222. return CGSizeMake(SCREEN_WIDTH, 41);
  223. }
  224. return CGSizeMake(SCREEN_WIDTH, 12);
  225. }
  226. //设置每个item的UIEdgeInsets
  227. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  228. {
  229. return UIEdgeInsetsMake(0, 0, 0, 0);
  230. }
  231. //设置每个item水平间距
  232. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  233. {
  234. return 0.f;
  235. }
  236. //设置每个item垂直间距
  237. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  238. {
  239. return 0.f;
  240. }
  241. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  242. if([kind isEqualToString:UICollectionElementKindSectionHeader]){
  243. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReusableView" forIndexPath:indexPath];
  244. [headerView removeAllSubviews];
  245. headerView.backgroundColor = UIColorHex(0xf7f7f7);
  246. if(indexPath.section == 0){
  247. UILabel *label1 = [[UILabel alloc] init];
  248. [headerView addSubview:label1];
  249. [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
  250. make.center.mas_equalTo(headerView);
  251. }];
  252. label1.text = @"长按拖动可以进行排序";//@"长按可拖动排序";
  253. label1.font = [UIFont systemFontOfSize:15];
  254. label1.textColor = UIColorHex(0x999999);
  255. }
  256. return headerView;
  257. }
  258. return nil;
  259. }
  260. //- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
  261. // WS(weakSelf);
  262. // if (destinationIndexPath.section == 0 && destinationIndexPath.item == 0) {
  263. // MoreAppModel *commonModel = [self.dataSourceArray firstObject];
  264. // NSMutableArray * commonArray = [NSMutableArray array];
  265. // for (MoreAppInfoModel * model in commonModel.moreAppInfoArray) {
  266. // NSDictionary * dict = [model modelToJSONObject];
  267. // [commonArray addObject:dict];
  268. // }
  269. // MoreAppModel *moreModel = [self.dataSourceArray lastObject];
  270. // NSMutableArray * moreArray = [NSMutableArray array];
  271. // for (MoreAppInfoModel * model in moreModel.moreAppInfoArray) {
  272. // NSDictionary * dict = [model modelToJSONObject];
  273. // [moreArray addObject:dict];
  274. // }
  275. // [[NSUserDefaults standardUserDefaults] setObject:@{@"common":commonArray,@"more":moreArray} forKey:CHATMENUBAR];
  276. // [[NSUserDefaults standardUserDefaults] synchronize];
  277. // dispatch_async(dispatch_get_main_queue(), ^{
  278. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  279. // [weakSelf.collectionView reloadData];
  280. // });
  281. // });
  282. // }else{
  283. // if (sourceIndexPath.section == 0) {
  284. // if (sourceIndexPath.item == 0) {
  285. // return;
  286. // }
  287. // MoreAppModel *moreModel = [self.dataSourceArray lastObject];
  288. // MoreAppModel *commonModel = [self.dataSourceArray firstObject];
  289. // MoreAppInfoModel * model = commonModel.moreAppInfoArray[sourceIndexPath.item];
  290. // [commonModel.moreAppInfoArray removeObjectAtIndex:sourceIndexPath.item];
  291. // if (destinationIndexPath.section == 0) {
  292. // [commonModel.moreAppInfoArray insertObject:model atIndex:destinationIndexPath.item];
  293. // }else{
  294. // [moreModel.moreAppInfoArray insertObject:model atIndex:destinationIndexPath.item];
  295. // }
  296. // }else{
  297. // MoreAppModel *moreModel = [self.dataSourceArray lastObject];
  298. // MoreAppModel *commonModel = [self.dataSourceArray firstObject];
  299. // MoreAppInfoModel * model = moreModel.moreAppInfoArray[sourceIndexPath.item];
  300. // [moreModel.moreAppInfoArray removeObjectAtIndex:sourceIndexPath.item];
  301. // if (destinationIndexPath.section == 0) {
  302. // [commonModel.moreAppInfoArray insertObject:model atIndex:destinationIndexPath.item];
  303. // }else{
  304. // [moreModel.moreAppInfoArray insertObject:model atIndex:destinationIndexPath.item];
  305. // }
  306. // }
  307. //
  308. // MoreAppModel *commonModel = [self.dataSourceArray firstObject];
  309. // NSMutableArray * commonArray = [NSMutableArray array];
  310. // for (MoreAppInfoModel * model in commonModel.moreAppInfoArray) {
  311. // NSDictionary * dict = [model modelToJSONObject];
  312. // [commonArray addObject:dict];
  313. // }
  314. // MoreAppModel *moreModel = [self.dataSourceArray lastObject];
  315. // NSMutableArray * moreArray = [NSMutableArray array];
  316. // for (MoreAppInfoModel * model in moreModel.moreAppInfoArray) {
  317. // NSDictionary * dict = [model modelToJSONObject];
  318. // [moreArray addObject:dict];
  319. // }
  320. // [[NSUserDefaults standardUserDefaults] setObject:@{@"common":commonArray,@"more":moreArray} forKey:CHATMENUBAR];
  321. // [[NSUserDefaults standardUserDefaults] synchronize];
  322. // dispatch_async(dispatch_get_main_queue(), ^{
  323. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  324. // [weakSelf.collectionView reloadData];
  325. // });
  326. // });
  327. // }
  328. //}
  329. -(NSMutableArray *)dataSourceArray{
  330. if(!_dataSourceArray){
  331. _dataSourceArray = [[NSMutableArray alloc] init];
  332. }
  333. return _dataSourceArray;
  334. }
  335. @end