2
0

DNPhotoBrowser.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. //
  2. // DNPhotoBrowserViewController.m
  3. // ImagePicker
  4. //
  5. // Created by DingXiao on 15/2/28.
  6. // Copyright (c) 2015年 Dennis. All rights reserved.
  7. //
  8. #import "DNPhotoBrowser.h"
  9. #import "UIView+DNImagePicker.h"
  10. #import "DNSendButton.h"
  11. #import "DNFullImageButton.h"
  12. #import "DNBrowserCell.h"
  13. #import "DNAsset.h"
  14. #import "DNImagePickerHelper.h"
  15. #import "WFCUImage.h"
  16. #pragma clang diagnostic push
  17. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  18. @interface DNPhotoBrowser () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
  19. {
  20. BOOL _statusBarShouldBeHidden;
  21. BOOL _didSavePreviousStateOfNavBar;
  22. BOOL _viewIsActive;
  23. BOOL _viewHasAppearedInitially;
  24. // Appearance
  25. BOOL _previousNavBarHidden;
  26. BOOL _previousNavBarTranslucent;
  27. UIBarStyle _previousNavBarStyle;
  28. UIColor *_previousNavBarTintColor;
  29. UIColor *_previousNavBarBarTintColor;
  30. UIBarButtonItem *_previousViewControllerBackButton;
  31. UIImage *_previousNavigationBarBackgroundImageDefault;
  32. UIImage *_previousNavigationBarBackgroundImageLandscapePhone;
  33. }
  34. @property (nonatomic, strong) UICollectionView *browserCollectionView;
  35. @property (nonatomic, strong) UIToolbar *toolbar;
  36. @property (nonatomic, strong) UIButton *checkButton;
  37. @property (nonatomic, strong) DNSendButton *sendButton;
  38. @property (nonatomic, strong) DNFullImageButton *fullImageButton;
  39. @property (nonatomic, strong) NSMutableArray *photoDataSources;
  40. @property (nonatomic, assign) NSInteger currentIndex;
  41. @property (nonatomic, getter=isFullImage) BOOL fullImage;
  42. @end
  43. @implementation DNPhotoBrowser
  44. - (instancetype)initWithPhotos:(NSArray *)photosArray
  45. currentIndex:(NSInteger)index
  46. fullImage:(BOOL)isFullImage {
  47. self = [super init];
  48. if (self) {
  49. _photoDataSources = [[NSMutableArray alloc] initWithArray:photosArray];
  50. _currentIndex = index;
  51. _fullImage = isFullImage;
  52. }
  53. return self;
  54. }
  55. - (void)viewDidLoad {
  56. [super viewDidLoad];
  57. [self setupView];
  58. [self updateSelestedNumber];
  59. [self updateNavigationBarAndToolBar];
  60. }
  61. - (void)viewWillAppear:(BOOL)animated {
  62. // Super
  63. [super viewWillAppear:animated];
  64. // Navigation bar appearance
  65. if (!_viewIsActive && [self.navigationController.viewControllers objectAtIndex:0] != self) {
  66. [self storePreviousNavBarAppearance];
  67. }
  68. // [self setNavBarAppearance:animated];
  69. // Initial appearance
  70. if (!_viewHasAppearedInitially) {
  71. _viewHasAppearedInitially = YES;
  72. }
  73. //scroll to the current offset
  74. [self.browserCollectionView setContentOffset:CGPointMake(self.browserCollectionView.frame.size.width * self.currentIndex,0)];
  75. }
  76. - (void)viewWillDisappear:(BOOL)animated {
  77. // Check that we're being popped for good
  78. if ([self.navigationController.viewControllers objectAtIndex:0] != self &&
  79. ![self.navigationController.viewControllers containsObject:self]) {
  80. _viewIsActive = NO;
  81. [self restorePreviousNavBarAppearance:animated];
  82. }
  83. [self.navigationController.navigationBar.layer removeAllAnimations];
  84. [NSObject cancelPreviousPerformRequestsWithTarget:self];
  85. [self setControlsHidden:NO animated:NO];
  86. // Super
  87. [super viewWillDisappear:animated];
  88. }
  89. - (UIStatusBarStyle)preferredStatusBarStyle {
  90. return UIStatusBarStyleDefault;
  91. }
  92. - (void)viewDidAppear:(BOOL)animated {
  93. [super viewDidAppear:animated];
  94. _viewIsActive = YES;
  95. }
  96. - (void)viewDidDisappear:(BOOL)animated {
  97. _viewIsActive = NO;
  98. [super viewDidDisappear:animated];
  99. }
  100. #pragma mark - priviate
  101. - (void)setupView {
  102. self.automaticallyAdjustsScrollViewInsets = NO;
  103. self.view.clipsToBounds = YES;
  104. [self browserCollectionView];
  105. [self toolbar];
  106. [self setupBarButtonItems];
  107. UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.checkButton];
  108. self.navigationItem.rightBarButtonItem = rightButtonItem;
  109. }
  110. - (void)setupData
  111. {
  112. self.photoDataSources = [NSMutableArray new];
  113. }
  114. - (void)setupBarButtonItems
  115. {
  116. UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:self.fullImageButton];
  117. UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  118. UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithCustomView:self.sendButton];
  119. UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  120. [self.toolbar setItems:@[item1,item2,item3,item4]];
  121. }
  122. - (void)updateNavigationBarAndToolBar {
  123. NSUInteger totalNumber = self.photoDataSources.count;
  124. self.title = [NSString stringWithFormat:@"%@/%@",@(self.currentIndex+1),@(totalNumber)];
  125. BOOL isSeleted = NO;
  126. if ([self.delegate respondsToSelector:@selector(photoBrowser:currentPhotoAssetIsSeleted:)]) {
  127. isSeleted = [self.delegate photoBrowser:self currentPhotoAssetIsSeleted:[self.photoDataSources objectAtIndex:self.currentIndex]];
  128. }
  129. self.checkButton.selected = isSeleted;
  130. self.fullImageButton.selected = self.isFullImage;
  131. if (self.isFullImage) {
  132. __weak typeof(self) wSelf = self;
  133. [DNImagePickerHelper fetchImageSizeWithAsset:self.photoDataSources[self.currentIndex] imageSizeResultHandler:^(CGFloat imageSize, NSString *sizeString) {
  134. __strong typeof(wSelf) sSelf = wSelf;
  135. sSelf.fullImageButton.text = sizeString;
  136. }];
  137. }
  138. }
  139. - (void)updateSelestedNumber
  140. {
  141. NSUInteger selectedNumber = 0;
  142. if ([self.delegate respondsToSelector:@selector(seletedPhotosNumberInPhotoBrowser:)]) {
  143. selectedNumber = [self.delegate seletedPhotosNumberInPhotoBrowser:self];
  144. }
  145. self.sendButton.badgeValue = [NSString stringWithFormat:@"%@",@(selectedNumber)];
  146. }
  147. #pragma mark - Nav Bar Appearance
  148. - (void)setNavBarAppearance:(BOOL)animated {
  149. [self.navigationController setNavigationBarHidden:NO animated:animated];
  150. UINavigationBar *navBar = self.navigationController.navigationBar;
  151. navBar.tintColor = [UIColor whiteColor];
  152. if ([navBar respondsToSelector:@selector(setBarTintColor:)]) {
  153. navBar.barTintColor = nil;
  154. navBar.shadowImage = nil;
  155. }
  156. navBar.translucent = YES;
  157. navBar.barStyle = UIBarStyleBlackTranslucent;
  158. if ([[UINavigationBar class] respondsToSelector:@selector(appearance)]) {
  159. [navBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
  160. [navBar setBackgroundImage:nil forBarMetrics:UIBarMetricsCompact];;
  161. }
  162. }
  163. - (void)storePreviousNavBarAppearance {
  164. _didSavePreviousStateOfNavBar = YES;
  165. if ([UINavigationBar instancesRespondToSelector:@selector(barTintColor)]) {
  166. _previousNavBarBarTintColor = self.navigationController.navigationBar.barTintColor;
  167. }
  168. _previousNavBarTranslucent = self.navigationController.navigationBar.translucent;
  169. _previousNavBarTintColor = self.navigationController.navigationBar.tintColor;
  170. _previousNavBarHidden = self.navigationController.navigationBarHidden;
  171. _previousNavBarStyle = self.navigationController.navigationBar.barStyle;
  172. if ([[UINavigationBar class] respondsToSelector:@selector(appearance)]) {
  173. _previousNavigationBarBackgroundImageDefault = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  174. _previousNavigationBarBackgroundImageLandscapePhone = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsCompact];
  175. }
  176. }
  177. - (void)restorePreviousNavBarAppearance:(BOOL)animated {
  178. if (_didSavePreviousStateOfNavBar) {
  179. [self.navigationController setNavigationBarHidden:_previousNavBarHidden animated:animated];
  180. UINavigationBar *navBar = self.navigationController.navigationBar;
  181. navBar.tintColor = _previousNavBarTintColor;
  182. navBar.translucent = _previousNavBarTranslucent;
  183. if ([UINavigationBar instancesRespondToSelector:@selector(barTintColor)]) {
  184. navBar.barTintColor = _previousNavBarBarTintColor;
  185. }
  186. navBar.barStyle = _previousNavBarStyle;
  187. if ([[UINavigationBar class] respondsToSelector:@selector(appearance)]) {
  188. [navBar setBackgroundImage:_previousNavigationBarBackgroundImageDefault forBarMetrics:UIBarMetricsDefault];
  189. [navBar setBackgroundImage:_previousNavigationBarBackgroundImageLandscapePhone forBarMetrics:UIBarMetricsCompact];
  190. }
  191. // Restore back button if we need to
  192. if (_previousViewControllerBackButton) {
  193. UIViewController *previousViewController = [self.navigationController topViewController]; // We've disappeared so previous is now top
  194. previousViewController.navigationItem.backBarButtonItem = _previousViewControllerBackButton;
  195. _previousViewControllerBackButton = nil;
  196. }
  197. }
  198. }
  199. #pragma mark - ui actions
  200. - (void)checkButtonAction {
  201. if (self.checkButton.selected) {
  202. if (self.delegate && [self.delegate respondsToSelector:@selector(photoBrowser:deseletedAsset:)]) {
  203. [self.delegate photoBrowser:self deseletedAsset:self.photoDataSources[self.currentIndex]];
  204. self.checkButton.selected = NO;
  205. }
  206. } else {
  207. if (self.delegate && [self.delegate respondsToSelector:@selector(photoBrowser:seletedAsset:)]) {
  208. self.checkButton.selected = [self.delegate photoBrowser:self seletedAsset:self.photoDataSources[self.currentIndex]];
  209. }
  210. }
  211. [self updateSelestedNumber];
  212. }
  213. - (void)backButtonAction {
  214. [self.navigationController popViewControllerAnimated:YES];
  215. }
  216. - (void)sendButtonAction {
  217. if ([self.delegate respondsToSelector:@selector(sendImagesFromPhotobrowser:currentAsset:)]) {
  218. [self.delegate sendImagesFromPhotobrowser:self currentAsset:self.photoDataSources[self.currentIndex]];
  219. }
  220. }
  221. - (void)fullImageButtonAction {
  222. self.fullImageButton.selected = !self.fullImageButton.selected;
  223. self.fullImage = self.fullImageButton.selected;
  224. if ([self.delegate respondsToSelector:@selector(photoBrowser:seleteFullImage:)]) {
  225. [self.delegate photoBrowser:self seleteFullImage:self.isFullImage];
  226. }
  227. if (self.fullImageButton.selected) {
  228. [self updateNavigationBarAndToolBar];
  229. BOOL success = [self.delegate photoBrowser:self seletedAsset:self.photoDataSources[self.currentIndex]];
  230. if (success) {
  231. [self updateSelestedNumber];
  232. [self updateNavigationBarAndToolBar];
  233. }
  234. }
  235. }
  236. #pragma mark - get/set
  237. - (UIButton *)checkButton {
  238. if (!_checkButton) {
  239. _checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
  240. _checkButton.frame = CGRectMake(0, 0, 25, 25);
  241. [_checkButton setBackgroundImage:[WFCUImage imageNamed:@"multi_selected"] forState:UIControlStateSelected];
  242. [_checkButton setBackgroundImage:[WFCUImage imageNamed:@"multi_unselected"] forState:UIControlStateNormal];
  243. [_checkButton addTarget:self action:@selector(checkButtonAction) forControlEvents:UIControlEventTouchUpInside];
  244. }
  245. return _checkButton;
  246. }
  247. - (DNFullImageButton *)fullImageButton {
  248. if (!_fullImageButton) {
  249. _fullImageButton = [[DNFullImageButton alloc] initWithFrame:CGRectZero];
  250. [_fullImageButton addTarget:self action:@selector(fullImageButtonAction)];
  251. _fullImageButton.selected = self.isFullImage;
  252. }
  253. return _fullImageButton;
  254. }
  255. - (DNSendButton *)sendButton {
  256. if (!_sendButton) {
  257. _sendButton = [[DNSendButton alloc] initWithFrame:CGRectZero];
  258. [_sendButton addTaget:self action:@selector(sendButtonAction)];
  259. }
  260. return _sendButton;
  261. }
  262. - (UIToolbar *)toolbar {
  263. if (!_toolbar) {
  264. CGFloat height = 44;
  265. _toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - height, self.view.bounds.size.width, height)];
  266. if ([[UIToolbar class] respondsToSelector:@selector(appearance)]) {
  267. [_toolbar setBackgroundImage:nil forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  268. [_toolbar setBackgroundImage:nil forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsCompact];
  269. }
  270. _toolbar.barStyle = UIBarStyleBlackTranslucent;
  271. _toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
  272. [self.view addSubview:_toolbar];
  273. }
  274. return _toolbar;
  275. }
  276. - (UICollectionView *)browserCollectionView {
  277. if (!_browserCollectionView) {
  278. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  279. layout.minimumInteritemSpacing = 0;
  280. layout.minimumLineSpacing = 0;
  281. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  282. _browserCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(-10, 0, self.view.bounds.size.width+20, self.view.bounds.size.height+1) collectionViewLayout:layout];
  283. _browserCollectionView.backgroundColor = [UIColor blackColor];
  284. [_browserCollectionView registerClass:[DNBrowserCell class] forCellWithReuseIdentifier:NSStringFromClass([DNBrowserCell class])];
  285. _browserCollectionView.delegate = self;
  286. _browserCollectionView.dataSource = self;
  287. _browserCollectionView.pagingEnabled = YES;
  288. _browserCollectionView.showsHorizontalScrollIndicator = NO;
  289. _browserCollectionView.showsVerticalScrollIndicator = NO;
  290. [self.view addSubview:_browserCollectionView];
  291. }
  292. return _browserCollectionView;
  293. }
  294. #pragma mark - UICollectionViewDataSource
  295. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  296. return [self.photoDataSources count];
  297. }
  298. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  299. DNBrowserCell *cell = (DNBrowserCell *)[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([DNBrowserCell class]) forIndexPath:indexPath];
  300. cell.asset = [self.photoDataSources objectAtIndex:indexPath.row];
  301. cell.photoBrowser = self;
  302. return cell;
  303. }
  304. #pragma mark - UICollectionViewDelegateFlowLayout
  305. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  306. return CGSizeMake(self.view.bounds.size.width+20, self.view.bounds.size.height);
  307. }
  308. #pragma mark - scrollerViewDelegate
  309. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  310. CGFloat offsetX = scrollView.contentOffset.x;
  311. CGFloat itemWidth = CGRectGetWidth(self.browserCollectionView.frame);
  312. CGFloat currentPageOffset = itemWidth * self.currentIndex;
  313. CGFloat deltaOffset = offsetX - currentPageOffset;
  314. if (fabs(deltaOffset) >= itemWidth/2 ) {
  315. [self.fullImageButton shouldAnimating:YES];
  316. } else {
  317. [self.fullImageButton shouldAnimating:NO];
  318. }
  319. }
  320. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  321. CGFloat offsetX = scrollView.contentOffset.x;
  322. CGFloat itemWidth = CGRectGetWidth(self.browserCollectionView.frame);
  323. if (offsetX >= 0){
  324. NSInteger page = offsetX / itemWidth;
  325. [self didScrollToPage:page];
  326. }
  327. [self.fullImageButton shouldAnimating:NO];
  328. }
  329. - (void)didScrollToPage:(NSInteger)page {
  330. self.currentIndex = page;
  331. [self updateNavigationBarAndToolBar];
  332. }
  333. #pragma mark - Control Hiding / Showing
  334. // Fades all controls slide and fade
  335. - (void)setControlsHidden:(BOOL)hidden animated:(BOOL)animated{
  336. // Force visible
  337. if (!self.photoDataSources || !self.photoDataSources.count)
  338. hidden = NO;
  339. // Animations & positions
  340. CGFloat animatonOffset = 20;
  341. CGFloat animationDuration = (animated ? 0.35 : 0);
  342. // Status bar
  343. if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
  344. // Hide status bar
  345. _statusBarShouldBeHidden = hidden;
  346. [UIView animateWithDuration:animationDuration animations:^(void) {
  347. [self setNeedsStatusBarAppearanceUpdate];
  348. } completion:^(BOOL finished) {}];
  349. }
  350. CGRect frame = CGRectIntegral(CGRectMake(0, self.view.bounds.size.height - 44, self.view.bounds.size.width, 44));
  351. // Pre-appear animation positions for iOS 7 sliding
  352. if ([self areControlsHidden] && !hidden && animated) {
  353. // Toolbar
  354. self.toolbar.frame = CGRectOffset(frame, 0, animatonOffset);
  355. }
  356. [UIView animateWithDuration:animationDuration animations:^(void) {
  357. CGFloat alpha = hidden ? 0 : 1;
  358. // Nav bar slides up on it's own on iOS 7
  359. [self.navigationController.navigationBar setAlpha:alpha];
  360. // Toolbar
  361. self.toolbar.frame = frame;
  362. if (hidden) self.toolbar.frame = CGRectOffset(self.toolbar.frame, 0, animatonOffset);
  363. self.toolbar.alpha = alpha;
  364. } completion:^(BOOL finished) {}];
  365. }
  366. - (BOOL)prefersStatusBarHidden {
  367. return _statusBarShouldBeHidden;
  368. }
  369. - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
  370. return UIStatusBarAnimationSlide;
  371. }
  372. - (BOOL)areControlsHidden { return (_toolbar.alpha == 0); }
  373. - (void)hideControls { [self setControlsHidden:YES animated:YES]; }
  374. - (void)toggleControls { [self setControlsHidden:![self areControlsHidden] animated:YES]; }
  375. @end
  376. #pragma clang diagnostic pop