JCHATPhotoBrowserViewController.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // HMPhotoBrowserViewController.m
  3. // HMPhotoPickerDemo
  4. //
  5. // Created by HuminiOS on 15/11/16.
  6. // Copyright © 2015年 HuminiOS. All rights reserved.
  7. //
  8. #import "JCHATPhotoBrowserViewController.h"
  9. #import "JCHATPhotoBrowserCollectionViewCell.h"
  10. #import "JCHATPhotoModel.h"
  11. #import "JCHATPhotoPickerConstants.h"
  12. @interface JCHATPhotoBrowserViewController ()<UICollectionViewDelegate,
  13. UICollectionViewDataSource,
  14. UICollectionViewDelegateFlowLayout,
  15. UIScrollViewDelegate> {
  16. __weak IBOutlet UICollectionView *collectionView;
  17. __weak IBOutlet UIView *topBar;
  18. __weak IBOutlet UIButton *selectStatusBtn;
  19. __weak IBOutlet UIView *bottomBar;
  20. }
  21. @end
  22. @implementation JCHATPhotoBrowserViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. self.collectionTop.constant = 64+statusbarHeight;
  26. self.topToolHeight.constant = 64+statusbarHeight;
  27. self.botToolHeight.constant = 45+ShowDiff;
  28. [self.navigationController setNavigationBarHidden:YES];
  29. self.navigationController.navigationBar.translucent = NO;
  30. topBar.backgroundColor = CellRow_Cor;
  31. bottomBar.backgroundColor = CellRow_Cor;
  32. [self setupCollectionView];
  33. }
  34. - (void)viewWillAppear:(BOOL)animated {
  35. [self.view layoutIfNeeded];
  36. [collectionView scrollToItemAtIndexPath:_currentIndex atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
  37. [self performSelector:@selector(updateSelectedBtn) withObject:nil afterDelay:0.1];
  38. }
  39. - (IBAction)ClickToSendImage:(id)sender {
  40. NSIndexPath *currentIndex = [self currentIndex];
  41. JCHATPhotoModel *currentPhotoModel = _allPhotoArr[currentIndex.item];
  42. currentPhotoModel.isSelected = YES;
  43. [self sendNotificationToFinishSelectPhoto:currentPhotoModel];
  44. }
  45. - (void)sendNotificationToFinishSelectPhoto:(JCHATPhotoModel *)currentPhotoModel {// 改为delegate
  46. [_selectVCDelegate finshToSelectPhoto:currentPhotoModel];
  47. }
  48. - (void)updateSelectedBtn {
  49. NSIndexPath *currentIndex = [self currentIndex];
  50. JCHATPhotoModel *currentPhotoModel = _allPhotoArr[currentIndex.item];
  51. selectStatusBtn.selected = currentPhotoModel.isSelected;
  52. _selectOriginBtn.selected = currentPhotoModel.isOriginPhoto;
  53. }
  54. - (void)setupCollectionView {
  55. UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
  56. [aFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
  57. collectionView.pagingEnabled = YES;
  58. [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"gradientCell"];
  59. [collectionView registerNib:[UINib nibWithNibName:@"JCHATPhotoBrowserCollectionViewCell" bundle:nil]
  60. forCellWithReuseIdentifier:@"JCHATPhotoBrowserCollectionViewCell"];
  61. collectionView.delegate = self;
  62. collectionView.dataSource = self;
  63. collectionView.userInteractionEnabled = YES;
  64. UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self
  65. action:@selector(tapContent:)];
  66. [collectionView addGestureRecognizer:gesture];
  67. }
  68. - (void)hidenAllBar {
  69. [UIView animateWithDuration:0.2 animations:^{
  70. topBar.hidden = !topBar.hidden;
  71. bottomBar.hidden = !bottomBar.hidden;
  72. }];
  73. }
  74. - (IBAction)ClickToBack:(id)sender {
  75. [self.navigationController popViewControllerAnimated:YES];
  76. }
  77. - (IBAction)ClickToSelectImage:(id)sender {
  78. UIButton *selectBtn = sender;
  79. JCHATPhotoModel *currentPhotoModel = _allPhotoArr[[self currentIndex].item];
  80. currentPhotoModel.isSelected = !currentPhotoModel.isSelected;
  81. selectBtn.selected = currentPhotoModel.isSelected;
  82. [_selectVCDelegate didSelectStatusChange:currentPhotoModel];
  83. }
  84. - (NSIndexPath *)currentIndex {
  85. NSInteger itemIndex = collectionView.contentOffset.x / collectionView.frame.size.width;
  86. NSInteger sectionIndex = 0;
  87. _currentIndex = [NSIndexPath indexPathForItem:itemIndex inSection:sectionIndex];
  88. return _currentIndex;
  89. }
  90. - (void)tapContent:(UIGestureRecognizer *)gesture {
  91. }
  92. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  93. return _allPhotoArr.count;
  94. }
  95. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  96. return 1;
  97. }
  98. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
  99. return [[UIScreen mainScreen] bounds].size;
  100. }
  101. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  102. static NSString *CellIdentifier = @"JCHATPhotoBrowserCollectionViewCell";
  103. JCHATPhotoBrowserCollectionViewCell *cell = (JCHATPhotoBrowserCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
  104. JCHATPhotoModel *currentPhotoModel = _allPhotoArr[indexPath.item];
  105. [cell setDataWithModel:currentPhotoModel];
  106. return cell;
  107. }
  108. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  109. NSIndexPath *currentIndex = [self currentIndex];
  110. JCHATPhotoModel *currentPhotoModel = _allPhotoArr[currentIndex.item];
  111. selectStatusBtn.selected = currentPhotoModel.isSelected;
  112. }
  113. - (IBAction)clickToSelectOriginImage:(id)sender {
  114. UIButton *selectOriginImageBtn = sender;
  115. JCHATPhotoModel *currentPhotoModel = _allPhotoArr[[self currentIndex].item];
  116. currentPhotoModel.isOriginPhoto = !currentPhotoModel.isOriginPhoto;
  117. selectOriginImageBtn.selected = currentPhotoModel.isOriginPhoto;
  118. }
  119. //- (void)layoutB
  120. @end