PCSPreviewViewController.m 32 KB

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