PCSPreviewViewController.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. //
  2. // PCSPreviewViewController.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by lr on 2023/3/11.
  6. //
  7. #import "PCSPreviewViewController.h"
  8. #import <Masonry/Masonry.h>
  9. #import "QuitMultipleModeAlertViewController.h"
  10. #import "UIImage+name.h"
  11. #import <AVFoundation/AVFoundation.h>
  12. #import <AVKit/AVKit.h>
  13. @interface PCSPreCollectionCell : UICollectionViewCell
  14. @property (nonatomic) LenzResourceItemModel *model;
  15. @property (nonatomic) UIImageView *imageView;
  16. @property (nonatomic) UIImageView *videoImageView;
  17. @property (nonatomic) AVPlayerViewController *player;
  18. @property (nonatomic) BOOL isPlayer;
  19. @end
  20. @implementation PCSPreCollectionCell
  21. - (instancetype)initWithFrame:(CGRect)frame {
  22. if (self = [super initWithFrame:frame]) {
  23. self.contentView.backgroundColor = [UIColor clearColor];
  24. [self.contentView addSubview:self.imageView];
  25. [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  26. make.edges.mas_offset(0);
  27. }];
  28. [self.contentView addSubview:self.videoImageView];
  29. [self.videoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  30. make.center.mas_equalTo(self.contentView);
  31. make.width.height.mas_offset(48);
  32. }];
  33. // player.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
  34. // player.view.hidden = YES;
  35. [self.contentView addSubview:self.player.view];
  36. }
  37. return self;
  38. }
  39. - (void)setModel:(LenzResourceItemModel *)model {
  40. _model = model;
  41. self.imageView.image = model.image;
  42. if (model.mode == SDK_CAPTURE_MODE_MOVIE) {
  43. self.videoImageView.hidden = NO;
  44. } else {
  45. self.videoImageView.hidden = YES;
  46. }
  47. }
  48. - (void)setIsPlayer:(BOOL)isPlayer {
  49. if (isPlayer) {
  50. AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:_model.path]];
  51. self.player.player = player;
  52. [self.player.player play];
  53. self.player.view.hidden = NO;
  54. } else {
  55. [self.player.player pause];
  56. self.player.view.hidden = YES;
  57. }
  58. }
  59. - (void)layoutSubviews {
  60. [super layoutSubviews];
  61. self.player.view.frame = self.contentView.bounds;
  62. }
  63. - (UIImageView *)imageView {
  64. if (!_imageView) {
  65. _imageView = [[UIImageView alloc]init];
  66. _imageView.layer.cornerRadius = 8;
  67. _imageView.layer.masksToBounds = YES;
  68. _imageView.backgroundColor = [UIColor blackColor];
  69. _imageView.contentMode = UIViewContentModeScaleAspectFit;
  70. }
  71. return _imageView;
  72. }
  73. - (UIImageView *)videoImageView {
  74. if (!_videoImageView) {
  75. _videoImageView = [[UIImageView alloc]init];
  76. _videoImageView.image = [UIImage loadNamed:@"icon_video"];
  77. _videoImageView.hidden = YES;
  78. }
  79. return _videoImageView;
  80. }
  81. - (AVPlayerViewController *)player {
  82. if (!_player) {
  83. _player = [[AVPlayerViewController alloc]init];
  84. _player.view.hidden = YES;
  85. _player.videoGravity = AVLayerVideoGravityResizeAspect;
  86. }
  87. return _player;
  88. }
  89. @end
  90. @interface PCSPreviewViewController ()<UIScrollViewDelegate, AVPlayerViewControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
  91. @property (nonatomic) UILabel *countLabel;
  92. @property (nonatomic) UIView *modeView;
  93. @property (nonatomic) UIScrollView *scrollView;
  94. @property (nonatomic) UILabel *curentLabel;
  95. @property (nonatomic) UIView *bottomView;
  96. @property (nonatomic) UIButton *backButton;
  97. @property (nonatomic) UILabel *backLabel;
  98. @property (nonatomic) UIButton *deleteButton;
  99. @property (nonatomic) UILabel *deleteLabel;
  100. @property (nonatomic) UIImageView *lineImageView;
  101. @property (nonatomic) NSInteger currentIndex;
  102. @property (nonatomic) UIImageView *videoImageView;
  103. @property (nonatomic) AVPlayerViewController *lastPlayer;
  104. //@property (nonatomic) NSInteger currentModeDataIndex;
  105. @property (nonatomic) NSMutableArray <AVPlayerViewController *> *allPlayer;
  106. @property (nonatomic) UICollectionView* collectionView;
  107. @property (nonatomic) NSIndexPath *playerIndexPath;
  108. @property (nonatomic) NSIndexPath *currentIndexPath;
  109. @end
  110. @implementation PCSPreviewViewController
  111. - (void)dealloc {
  112. }
  113. - (void)viewDidLoad {
  114. [super viewDidLoad];
  115. self.allPlayer = [NSMutableArray array];
  116. self.view.backgroundColor = [UIColor colorWithRed:60/255.0 green:58/255.0 blue:61/255.0 alpha:1];
  117. [self.view addSubview:self.countLabel];
  118. CGFloat top = UIApplication.sharedApplication.delegate.window.safeAreaInsets.top;
  119. [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  120. make.top.mas_offset(top + 20);
  121. make.right.mas_offset(-20);
  122. make.height.mas_offset(44);
  123. }];
  124. [self.view addSubview:self.modeView];
  125. [self.modeView mas_makeConstraints:^(MASConstraintMaker *make) {
  126. make.left.mas_offset(0);
  127. make.top.mas_equalTo(self.countLabel);
  128. make.height.mas_equalTo(self.countLabel);
  129. make.right.mas_equalTo(self.countLabel.mas_left).mas_offset(-10);
  130. }];
  131. [self setupModeView];
  132. [self.view addSubview:self.bottomView];
  133. [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  134. make.left.right.bottom.mas_offset(0);
  135. make.height.mas_offset(150);
  136. }];
  137. [self.bottomView addSubview:self.backLabel];
  138. [self.backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  139. make.bottom.mas_offset(-30);
  140. make.centerX.mas_equalTo(self.bottomView).mas_offset(-60);
  141. }];
  142. [self.bottomView addSubview:self.backButton];
  143. [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
  144. make.bottom.mas_equalTo(self.backLabel.mas_top).mas_offset(-20);
  145. make.centerX.mas_equalTo(self.backLabel);
  146. make.width.height.mas_offset(60);
  147. }];
  148. [self.bottomView addSubview:self.deleteLabel];
  149. [self.deleteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  150. make.bottom.mas_offset(-30);
  151. make.centerX.mas_equalTo(self.bottomView).mas_offset(60);
  152. }];
  153. [self.bottomView addSubview:self.deleteButton];
  154. [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
  155. make.bottom.mas_equalTo(self.deleteLabel.mas_top).mas_offset(-20);
  156. make.centerX.mas_equalTo(self.deleteLabel);
  157. make.width.height.mas_offset(60);
  158. }];
  159. UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc]init];
  160. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  161. layout.minimumLineSpacing = 0;
  162. layout.minimumInteritemSpacing = 0;
  163. layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, [UIScreen mainScreen].bounds.size.height - top - 224);
  164. self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
  165. self.collectionView.delegate = self;
  166. self.collectionView.dataSource = self;
  167. self.collectionView.pagingEnabled = YES;
  168. self.collectionView.backgroundColor = [UIColor clearColor];
  169. self.collectionView.hidden = YES;
  170. // 注册item类型
  171. [self.collectionView registerClass:[PCSPreCollectionCell class] forCellWithReuseIdentifier:@"PCSPreCollectionCell"];
  172. [self.view addSubview:self.collectionView];
  173. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  174. make.left.mas_offset(20);
  175. make.right.mas_offset(-20);
  176. make.top.mas_equalTo(self.countLabel.mas_bottom).mas_offset(10);
  177. make.bottom.mas_equalTo(self.bottomView.mas_top);
  178. }];
  179. self.currentIndexPath = nil;
  180. if (self.model.continousArray.count > 0 && self.selectIndex == SDK_CAPTURE_MODE_CONTINUOUS) {
  181. self.currentIndex = 1;
  182. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.continousArray.count - 1 inSection:0];
  183. }
  184. if (self.model.movieArray.count > 0 && self.selectIndex == SDK_CAPTURE_MODE_MOVIE) {
  185. self.currentIndex = 2;
  186. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.movieArray.count - 1 inSection:1];
  187. }
  188. if (self.model.panoramArray.count > 0 && self.selectIndex == SDK_CAPTURE_MODE_PANORAMA) {
  189. self.currentIndex = 3;
  190. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.panoramArray.count - 1 inSection:2];
  191. }
  192. if (self.model.aiPanoramArray.count > 0 && self.selectIndex == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  193. self.currentIndex = 4;
  194. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.aiPanoramArray.count - 1 inSection:3];
  195. }
  196. if(!self.currentIndexPath) {
  197. if (self.model.aiPanoramArray.count > 0) {
  198. self.currentIndex = 4;
  199. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.aiPanoramArray.count - 1 inSection:3];
  200. } else if (self.model.panoramArray.count > 0) {
  201. self.currentIndex = 3;
  202. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.panoramArray.count - 1 inSection:2];
  203. } else if (self.model.movieArray.count > 0) {
  204. self.currentIndex = 2;
  205. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.movieArray.count - 1 inSection:1];
  206. } else {
  207. self.currentIndex = 1;
  208. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.continousArray.count - 1 inSection:0];
  209. }
  210. }
  211. [self changeLabelStatusWith:self.currentIndex];
  212. [self updateCountLabelWith:self.currentIndexPath];
  213. [self.view addSubview:self.curentLabel];
  214. [self.curentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  215. make.centerX.mas_equalTo(self.view);
  216. make.width.mas_offset(100);
  217. make.height.mas_offset(32);
  218. make.bottom.mas_equalTo(self.collectionView).mas_offset(16);
  219. }];
  220. }
  221. - (void)viewDidAppear:(BOOL)animated {
  222. [super viewDidAppear:animated];
  223. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  224. self.collectionView.hidden = NO;
  225. UICollectionViewLayoutAttributes*attributes = [self.collectionView layoutAttributesForItemAtIndexPath:self.currentIndexPath];
  226. CGRect rect = attributes.frame;
  227. [self.collectionView setContentOffset:CGPointMake(rect.origin.x, rect.origin.y ) animated:NO];
  228. });
  229. }
  230. - (UIImageView *)lineImageView {
  231. if (!_lineImageView) {
  232. _lineImageView = [[UIImageView alloc]init];
  233. _lineImageView.image = [UIImage loadNamed:@"icon_preview_line"];
  234. }
  235. return _lineImageView;
  236. }
  237. - (void)updateCountLabelWith:(NSIndexPath *)indexPath {
  238. if (!indexPath) {
  239. return;
  240. }
  241. if (indexPath.section == 0) {
  242. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld张", self.currentIndexPath.row + 1, self.model.continousArray.count];
  243. } else if (indexPath.section == 1) {
  244. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld条", self.currentIndexPath.row + 1, self.model.movieArray.count];
  245. } else if (indexPath.section == 2) {
  246. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld张", self.currentIndexPath.row + 1, self.model.panoramArray.count];
  247. } else {
  248. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld张", self.currentIndexPath.row + 1, self.model.aiPanoramArray.count];
  249. }
  250. }
  251. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  252. return 4;
  253. }
  254. //返回每个分区的item个数
  255. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  256. if (section == 0) {
  257. return self.model.continousArray.count;
  258. } else if (section == 1) {
  259. return self.model.movieArray.count;
  260. } else if (section == 2) {
  261. return self.model.panoramArray.count;
  262. } else {
  263. return self.model.aiPanoramArray.count;
  264. }
  265. }
  266. //返回每个item
  267. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  268. PCSPreCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PCSPreCollectionCell" forIndexPath:indexPath];
  269. cell.isPlayer = NO;
  270. if (indexPath.section == 0) {
  271. if (self.model.continousArray.count > indexPath.row) {
  272. LenzResourceItemModel *model = self.model.continousArray[indexPath.row];
  273. cell.model = model;
  274. }
  275. } else if (indexPath.section == 1) {
  276. if (self.model.movieArray.count > indexPath.row) {
  277. LenzResourceItemModel *model = self.model.movieArray[indexPath.row];
  278. cell.model = model;
  279. cell.isPlayer = indexPath == self.playerIndexPath;
  280. }
  281. } else if (indexPath.section == 2) {
  282. if (self.model.panoramArray.count > indexPath.row) {
  283. LenzResourceItemModel *model = self.model.panoramArray[indexPath.row];
  284. cell.model = model;
  285. }
  286. } else if (indexPath.section == 3) {
  287. if (self.model.aiPanoramArray.count > indexPath.row) {
  288. LenzResourceItemModel *model = self.model.aiPanoramArray[indexPath.row];
  289. cell.model = model;
  290. }
  291. }
  292. return cell;
  293. }
  294. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  295. if (indexPath.section == 1) {
  296. self.playerIndexPath = indexPath;
  297. [self.collectionView reloadData];
  298. }
  299. }
  300. // 监听UIScrollView的滑动停止
  301. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  302. self.playerIndexPath = nil;
  303. NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
  304. NSIndexPath *currentIndexPath = indexPaths.firstObject;
  305. self.currentIndexPath = currentIndexPath;
  306. [self changeLabelStatusWith:currentIndexPath.section + 1];
  307. [self updateCountLabelWith:currentIndexPath];
  308. }
  309. - (void)updateCurrentIndexPath {
  310. NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
  311. NSIndexPath *currentIndexPath = indexPaths.firstObject;
  312. self.currentIndexPath = currentIndexPath;
  313. [self changeLabelStatusWith:currentIndexPath.section + 1];
  314. [self updateCountLabelWith:currentIndexPath];
  315. }
  316. - (void)updateViewWhenDelete {
  317. self.playerIndexPath = nil;
  318. NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
  319. NSIndexPath *currentIndexPath = indexPaths.firstObject;
  320. self.currentIndexPath = currentIndexPath;
  321. [self changeLabelStatusWith:currentIndexPath.section + 1];
  322. [self updateCountLabelWith:currentIndexPath];
  323. }
  324. - (void)setupModeView {
  325. [self.modeView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  326. CGFloat margin = [UIScreen mainScreen].bounds.size.width == 375 ? 20 : 30;
  327. UIView *lastView = nil;
  328. NSInteger count = 0;
  329. self.currentIndex = 0;
  330. if (self.model.continousArray.count) {
  331. count += self.model.continousArray.count;
  332. self.currentIndex = 1;
  333. UILabel *label = [[UILabel alloc]init];
  334. label.text = @"连拍";
  335. label.tag = 1;
  336. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  337. [label addGestureRecognizer:tap];
  338. label.userInteractionEnabled = YES;
  339. label.textColor = [UIColor whiteColor];
  340. [self.modeView addSubview:label];
  341. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  342. make.left.mas_offset(20);
  343. make.centerY.mas_equalTo(self.countLabel);
  344. }];
  345. lastView = label;
  346. }
  347. if (self.model.movieArray.count) {
  348. count += self.model.movieArray.count;
  349. if (self.currentIndex == 0) {
  350. self.currentIndex = 2;
  351. }
  352. UILabel *label = [[UILabel alloc]init];
  353. label.text = @"视频";
  354. label.tag = 2;
  355. label.textColor = [UIColor whiteColor];
  356. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  357. [label addGestureRecognizer:tap];
  358. label.userInteractionEnabled = YES;
  359. [self.modeView addSubview:label];
  360. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  361. if (lastView) {
  362. make.left.mas_equalTo(lastView.mas_right).mas_offset(margin);
  363. } else {
  364. make.left.mas_equalTo(20);
  365. }
  366. make.centerY.mas_equalTo(self.countLabel);
  367. }];
  368. lastView = label;
  369. }
  370. if (self.model.panoramArray.count) {
  371. count += self.model.panoramArray.count;
  372. if (self.currentIndex == 0) {
  373. self.currentIndex = 3;
  374. }
  375. UILabel *label = [[UILabel alloc]init];
  376. label.text = @"全景";
  377. label.tag = 3;
  378. label.textColor = [UIColor whiteColor];
  379. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  380. [label addGestureRecognizer:tap];
  381. label.userInteractionEnabled = YES;
  382. [self.modeView addSubview:label];
  383. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  384. if (lastView) {
  385. make.left.mas_equalTo(lastView.mas_right).mas_offset(margin);
  386. } else {
  387. make.left.mas_equalTo(20);
  388. }
  389. make.centerY.mas_equalTo(self.countLabel);
  390. }];
  391. lastView = label;
  392. }
  393. if (self.model.aiPanoramArray.count) {
  394. count += self.model.aiPanoramArray.count;
  395. if (self.currentIndex == 0) {
  396. self.currentIndex = 4;
  397. }
  398. UILabel *label = [[UILabel alloc]init];
  399. label.text = @"智能全景";
  400. label.tag = 4;
  401. label.textColor = [UIColor whiteColor];
  402. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  403. [label addGestureRecognizer:tap];
  404. label.userInteractionEnabled = YES;
  405. [self.modeView addSubview:label];
  406. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  407. if (lastView) {
  408. make.left.mas_equalTo(lastView.mas_right).mas_offset(margin);
  409. } else {
  410. make.left.mas_equalTo(20);
  411. }
  412. make.centerY.mas_equalTo(self.countLabel);
  413. }];
  414. lastView = label;
  415. }
  416. self.countLabel.text = [NSString stringWithFormat:@"共%ld笔数据", count];
  417. [self.modeView addSubview:self.lineImageView];
  418. }
  419. - (void)changeAction:(UITapGestureRecognizer *)tap {
  420. [self.view layoutIfNeeded];
  421. UILabel *label = (UILabel *)tap.view;
  422. self.currentIndexPath = [NSIndexPath indexPathForItem:0 inSection:label.tag - 1];
  423. UICollectionViewLayoutAttributes*attributes = [self.collectionView layoutAttributesForItemAtIndexPath:self.currentIndexPath];
  424. CGRect rect = attributes.frame;
  425. [self.collectionView setContentOffset:CGPointMake(rect.origin.x, rect.origin.y ) animated:NO];
  426. [self changeLabelStatusWith:label.tag];
  427. [self updateCountLabelWith:self.currentIndexPath];
  428. }
  429. - (void)changeLabelStatusWith:(NSInteger)index {
  430. if(index <=0) {
  431. return;
  432. }
  433. UILabel *label1 = [self.modeView viewWithTag:1];
  434. if (label1) {
  435. label1.textColor = [UIColor whiteColor];
  436. }
  437. UILabel *label2 = [self.modeView viewWithTag:2];
  438. if (label2) {
  439. label2.textColor = [UIColor whiteColor];
  440. }
  441. UILabel *label3 = [self.modeView viewWithTag:3];
  442. if (label3) {
  443. label3.textColor = [UIColor whiteColor];
  444. }
  445. UILabel *label4 = [self.modeView viewWithTag:4];
  446. if (label4) {
  447. label4.textColor = [UIColor whiteColor];
  448. }
  449. UILabel *label = (UILabel *)[self.modeView viewWithTag:index];
  450. if (label) {
  451. label.textColor = [UIColor colorWithRed:231/255.0 green:108/255.0 blue:30/255.0 alpha:1];
  452. [self.lineImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  453. make.top.mas_equalTo(label.mas_bottom);
  454. make.centerX.mas_equalTo(label);
  455. make.width.mas_offset(18);
  456. make.height.mas_offset(9);
  457. }];
  458. }
  459. self.currentIndex = index;
  460. }
  461. - (void)updateScrollWith:(NSInteger)tag {
  462. if (tag > 0) {
  463. NSArray <LenzResourceItemModel *> *source = nil;
  464. if (tag == 1) {
  465. source = self.model.continousArray;
  466. } else if (tag == 2) {
  467. source = self.model.movieArray;
  468. } else if (tag == 3) {
  469. source = self.model.panoramArray;
  470. } else if (tag == 4) {
  471. source = self.model.aiPanoramArray;
  472. }
  473. [self updateScrollViewWith:source];
  474. }
  475. }
  476. - (void)updateScrollViewWith:(NSArray <LenzResourceItemModel *> *)array {
  477. CGFloat width = CGRectGetWidth(self.scrollView.frame);
  478. CGFloat height = CGRectGetHeight(self.scrollView.frame);
  479. [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  480. self.scrollView.pagingEnabled = YES;
  481. self.scrollView.contentSize = CGSizeMake(width * array.count, height);
  482. __block UIImageView *lastImageView = nil;
  483. [array enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  484. UIImageView *imageView = [[UIImageView alloc]init];
  485. imageView.image = obj.image;
  486. imageView.tag = idx;
  487. [self.scrollView addSubview:imageView];
  488. [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  489. make.top.mas_offset(0);
  490. make.height.mas_offset(height);
  491. make.width.mas_offset(width);
  492. if (lastImageView) {
  493. make.left.mas_equalTo(lastImageView.mas_right);
  494. } else {
  495. make.left.mas_offset(0);
  496. }
  497. }];
  498. UIImageView *videoImageView = [[UIImageView alloc]init];
  499. videoImageView.image = [UIImage loadNamed:@"icon_video"];
  500. [imageView addSubview:videoImageView];
  501. [videoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  502. make.center.mas_equalTo(imageView);
  503. make.width.height.mas_offset(48);
  504. }];
  505. [self.view setNeedsLayout];
  506. [self.view layoutIfNeeded];
  507. if (obj.mode == SDK_CAPTURE_MODE_MOVIE) {
  508. videoImageView.hidden = NO;
  509. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(videoAction:)];
  510. [imageView addGestureRecognizer:tap];
  511. imageView.userInteractionEnabled = YES;
  512. AVPlayerViewController *player = [[AVPlayerViewController alloc]init];
  513. player.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
  514. player.view.hidden = YES;
  515. [imageView addSubview:player.view];
  516. [self.allPlayer addObject:player];
  517. } else {
  518. videoImageView.hidden = YES;
  519. }
  520. lastImageView = imageView;
  521. }];
  522. self.curentLabel.text = [NSString stringWithFormat:@"第%d/%ld条", 1, array.count];
  523. }
  524. - (void)videoAction:(UITapGestureRecognizer *)tap {
  525. UIImageView *view = (UIImageView *)tap.view;
  526. if (self.model.movieArray.count > view.tag) {
  527. LenzResourceItemModel *model = self.model.movieArray[view.tag];
  528. AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:model.path]];
  529. if (self.lastPlayer) {
  530. [self.lastPlayer.player pause];
  531. self.lastPlayer.player = nil;
  532. self.lastPlayer.view.hidden = YES;
  533. }
  534. AVPlayerViewController *playerVC = self.allPlayer[view.tag];
  535. playerVC.view.hidden = NO;
  536. playerVC.player = player;
  537. [playerVC.player play];
  538. self.lastPlayer = playerVC;
  539. }
  540. }
  541. - (void)backAction {
  542. [self dismissViewControllerAnimated:YES completion:nil];
  543. }
  544. - (void)deleteAction {
  545. NSString *title = @"图片删除后无法恢复,请确认!";
  546. if (self.currentIndex == 2) {
  547. title = @"视频删除后无法恢复,请确认!";
  548. }
  549. [QuitMultipleModeAlertViewController show:self title:@"确认提醒" text:title leftBtnTitle:@"取消" rightBtnTitle:@"确定" withLeftButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  550. [alertController dismissViewControllerAnimated:YES completion:nil];
  551. } rightButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  552. NSIndexPath *showIndexPath = nil;
  553. if (self.currentIndex == 1) {
  554. if (self.model.continousArray.count > self.currentIndexPath.item) {
  555. [self.model.continousArray removeObjectAtIndex:self.currentIndexPath.item];
  556. }
  557. if (self.model.continousArray.count > 0 ) {
  558. showIndexPath = [NSIndexPath indexPathForRow:self.currentIndexPath.item - 1 inSection:self.currentIndexPath.section];
  559. }
  560. } else if (self.currentIndex == 2) {
  561. if (self.model.movieArray.count > self.currentIndexPath.item) {
  562. [self.model.movieArray removeObjectAtIndex:self.currentIndexPath.item];
  563. }
  564. if (self.model.movieArray.count > 0 ) {
  565. showIndexPath = [NSIndexPath indexPathForRow:self.currentIndexPath.item - 1 inSection:self.currentIndexPath.section];
  566. }
  567. } else if (self.currentIndex == 3) {
  568. if (self.model.panoramArray.count > self.currentIndexPath.item) {
  569. [self.model.panoramArray removeObjectAtIndex:self.currentIndexPath.item];
  570. }
  571. if (self.model.panoramArray.count > 0 ) {
  572. showIndexPath = [NSIndexPath indexPathForRow:self.currentIndexPath.item - 1 inSection:self.currentIndexPath.section];
  573. }
  574. } else if (self.currentIndex == 4) {
  575. if (self.model.aiPanoramArray.count > self.currentIndexPath.item) {
  576. [self.model.aiPanoramArray removeObjectAtIndex:self.currentIndexPath.item];
  577. }
  578. if (self.model.aiPanoramArray.count > 0 ) {
  579. showIndexPath = [NSIndexPath indexPathForRow:self.currentIndexPath.item - 1 inSection:self.currentIndexPath.section];
  580. }
  581. }
  582. if(showIndexPath) {
  583. UICollectionViewLayoutAttributes*attributes = [self.collectionView layoutAttributesForItemAtIndexPath:showIndexPath];
  584. CGRect rect = attributes.frame;
  585. [self.collectionView reloadData];
  586. [self.collectionView setContentOffset:CGPointMake(rect.origin.x, rect.origin.y ) animated:NO];
  587. } else {
  588. [self setupModeView];
  589. [self scrollViewDidEndDecelerating:self.collectionView];
  590. [self.collectionView reloadData];
  591. }
  592. [self.view setNeedsLayout];
  593. [self.view layoutIfNeeded];
  594. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  595. [self updateCurrentIndexPath];
  596. });
  597. [self updateViewWhenDelete];
  598. if (self.dataChangeBlock) {
  599. self.dataChangeBlock();
  600. }
  601. if (self.model.continousArray.count == 0 &&
  602. self.model.movieArray.count == 0 &&
  603. self.model.panoramArray.count == 0 &&
  604. self.model.aiPanoramArray.count == 0) {
  605. self.curentLabel.hidden = YES;
  606. [alertController dismissViewControllerAnimated:YES completion:^{
  607. [self dismissViewControllerAnimated:YES completion:nil];
  608. }];
  609. } else {
  610. self.curentLabel.hidden = NO;
  611. [alertController dismissViewControllerAnimated:YES completion:nil];
  612. }
  613. }];
  614. }
  615. - (UILabel *)countLabel {
  616. if (!_countLabel) {
  617. _countLabel = [[UILabel alloc]init];
  618. _countLabel.font = [UIFont systemFontOfSize:14];
  619. _countLabel.textColor = [UIColor colorWithRed:231/255.0 green:108/255.0 blue:30/255.0 alpha:1];
  620. }
  621. return _countLabel;
  622. }
  623. - (UIView *)bottomView {
  624. if (!_bottomView) {
  625. _bottomView = [[UIView alloc]init];
  626. }
  627. return _bottomView;
  628. }
  629. - (UIView *)modeView {
  630. if (!_modeView) {
  631. _modeView = [[UIView alloc]init];
  632. _modeView.userInteractionEnabled = YES;
  633. }
  634. return _modeView;
  635. }
  636. - (UIScrollView *)scrollView {
  637. if (!_scrollView) {
  638. _scrollView = [[UIScrollView alloc]init];
  639. _scrollView.layer.cornerRadius = 8;
  640. _scrollView.layer.masksToBounds = YES;
  641. _scrollView.delegate = self;
  642. _scrollView.backgroundColor = [UIColor blackColor];
  643. }
  644. return _scrollView;
  645. }
  646. - (UIButton *)backButton {
  647. if (!_backButton) {
  648. _backButton = [[UIButton alloc]init];
  649. [_backButton setImage:[UIImage loadNamed:@"result-return-btn"] forState:UIControlStateNormal];
  650. [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  651. }
  652. return _backButton;
  653. }
  654. - (UILabel *)backLabel {
  655. if (!_backLabel) {
  656. _backLabel = [[UILabel alloc]init];
  657. _backLabel.text = @"返回";
  658. _backLabel.textColor = [UIColor whiteColor];
  659. }
  660. return _backLabel;
  661. }
  662. - (UILabel *)deleteLabel {
  663. if (!_deleteLabel) {
  664. _deleteLabel = [[UILabel alloc]init];
  665. _deleteLabel.text = @"删除";
  666. _deleteLabel.textColor = [UIColor whiteColor];
  667. }
  668. return _deleteLabel;
  669. }
  670. - (UIButton *)deleteButton {
  671. if (!_deleteButton) {
  672. _deleteButton = [[UIButton alloc]init];
  673. [_deleteButton setImage:[UIImage loadNamed:@"result-delete-btn"] forState:UIControlStateNormal];
  674. [_deleteButton addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
  675. }
  676. return _deleteButton;
  677. }
  678. - (UILabel *)curentLabel {
  679. if (!_curentLabel) {
  680. _curentLabel = [[UILabel alloc]init];
  681. _curentLabel.backgroundColor = [UIColor colorWithRed:231/255.0 green:108/255.0 blue:30/255.0 alpha:1];
  682. _curentLabel.layer.cornerRadius = 16;
  683. _curentLabel.layer.masksToBounds = YES;
  684. _curentLabel.textAlignment = NSTextAlignmentCenter;
  685. _curentLabel.textColor = [UIColor whiteColor];
  686. }
  687. return _curentLabel;
  688. }
  689. - (UIImageView *)videoImageView {
  690. if (!_videoImageView) {
  691. _videoImageView = [[UIImageView alloc]init];
  692. _videoImageView.image = [UIImage loadNamed:@"icon_video"];
  693. }
  694. return _videoImageView;
  695. }
  696. @end