MWPhotoBrowser.m 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. //
  2. // MWPhotoBrowser.m
  3. // MWPhotoBrowser
  4. //
  5. // Created by Michael Waterfall on 14/10/2010.
  6. // Copyright 2010 d3i. All rights reserved.
  7. //
  8. #import <QuartzCore/QuartzCore.h>
  9. #import "MWCommon.h"
  10. #import "MWPhotoBrowser.h"
  11. #import "MWPhotoBrowserPrivate.h"
  12. #import "UIImage+MWPhotoBrowser.h"
  13. #import "NCBridgeSwift.h"
  14. #define PADDING 10
  15. #pragma clang diagnostic ignored "-Wundeclared-selector"
  16. static void * MWVideoPlayerObservation = &MWVideoPlayerObservation;
  17. @implementation MWPhotoBrowser
  18. #pragma mark - Init
  19. - (id)init {
  20. if ((self = [super init])) {
  21. [self _initialisation];
  22. }
  23. return self;
  24. }
  25. - (id)initWithDelegate:(id <MWPhotoBrowserDelegate>)delegate {
  26. if ((self = [self init])) {
  27. _delegate = delegate;
  28. }
  29. return self;
  30. }
  31. - (id)initWithPhotos:(NSArray *)photosArray {
  32. if ((self = [self init])) {
  33. _fixedPhotosArray = photosArray;
  34. }
  35. return self;
  36. }
  37. - (id)initWithCoder:(NSCoder *)decoder {
  38. if ((self = [super initWithCoder:decoder])) {
  39. [self _initialisation];
  40. }
  41. return self;
  42. }
  43. - (void)_initialisation {
  44. // Defaults
  45. NSNumber *isVCBasedStatusBarAppearanceNum = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"];
  46. if (isVCBasedStatusBarAppearanceNum) {
  47. _isVCBasedStatusBarAppearance = isVCBasedStatusBarAppearanceNum.boolValue;
  48. } else {
  49. _isVCBasedStatusBarAppearance = YES; // default
  50. }
  51. self.hidesBottomBarWhenPushed = YES;
  52. _hasBelongedToViewController = NO;
  53. _photoCount = NSNotFound;
  54. _previousLayoutBounds = CGRectZero;
  55. _currentPageIndex = 0;
  56. _previousPageIndex = NSUIntegerMax;
  57. _displayActionButton = YES;
  58. _displayShareButton = YES;
  59. _displayDeleteButton = YES;
  60. _displayNavArrows = NO;
  61. _zoomPhotosToFill = YES;
  62. _performingLayout = NO; // Reset on view did appear
  63. _rotating = NO;
  64. _viewIsActive = NO;
  65. _enableGrid = YES;
  66. _startOnGrid = NO;
  67. _enableSwipeToDismiss = YES;
  68. _delayToHideElements = 5;
  69. _visiblePages = [[NSMutableSet alloc] init];
  70. _recycledPages = [[NSMutableSet alloc] init];
  71. _photos = [[NSMutableArray alloc] init];
  72. _thumbPhotos = [[NSMutableArray alloc] init];
  73. _currentGridContentOffset = CGPointMake(0, CGFLOAT_MAX);
  74. _didSavePreviousStateOfNavBar = NO;
  75. self.automaticallyAdjustsScrollViewInsets = NO;
  76. // Listen for MWPhoto notifications
  77. [[NSNotificationCenter defaultCenter] addObserver:self
  78. selector:@selector(handleMWPhotoLoadingDidEndNotification:)
  79. name:MWPHOTO_LOADING_DID_END_NOTIFICATION
  80. object:nil];
  81. }
  82. - (void)dealloc {
  83. [self clearCurrentVideo];
  84. _pagingScrollView.delegate = nil;
  85. [[NSNotificationCenter defaultCenter] removeObserver:self];
  86. [self releaseAllUnderlyingPhotos:NO];
  87. }
  88. - (void)releaseAllUnderlyingPhotos:(BOOL)preserveCurrent {
  89. // Create a copy in case this array is modified while we are looping through
  90. // Release photos
  91. NSArray *copy = [_photos copy];
  92. for (id p in copy) {
  93. if (p != [NSNull null]) {
  94. if (preserveCurrent && p == [self photoAtIndex:self.currentIndex]) {
  95. continue; // skip current
  96. }
  97. [p unloadUnderlyingImage];
  98. }
  99. }
  100. // Release thumbs
  101. copy = [_thumbPhotos copy];
  102. for (id p in copy) {
  103. if (p != [NSNull null]) {
  104. [p unloadUnderlyingImage];
  105. }
  106. }
  107. }
  108. - (void)didReceiveMemoryWarning {
  109. // Release any cached data, images, etc that aren't in use.
  110. [self releaseAllUnderlyingPhotos:YES];
  111. [_recycledPages removeAllObjects];
  112. // Releases the view if it doesn't have a superview.
  113. [super didReceiveMemoryWarning];
  114. }
  115. #pragma mark - View Loading
  116. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  117. - (void)viewDidLoad {
  118. // Validate grid settings
  119. if (_startOnGrid) _enableGrid = YES;
  120. if (_enableGrid) {
  121. _enableGrid = [_delegate respondsToSelector:@selector(photoBrowser:thumbPhotoAtIndex:)];
  122. }
  123. if (!_enableGrid) _startOnGrid = NO;
  124. // View
  125. self.view.backgroundColor = [UIColor whiteColor]; //TWS
  126. self.view.clipsToBounds = YES;
  127. // Setup paging scrolling view
  128. CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
  129. _pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
  130. _pagingScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  131. _pagingScrollView.pagingEnabled = YES;
  132. _pagingScrollView.delegate = self;
  133. _pagingScrollView.showsHorizontalScrollIndicator = NO;
  134. _pagingScrollView.showsVerticalScrollIndicator = NO;
  135. _pagingScrollView.backgroundColor = [UIColor whiteColor]; //TWS
  136. _pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
  137. [self.view addSubview:_pagingScrollView];
  138. // Toolbar
  139. _toolbar = [[UIToolbar alloc] initWithFrame:[self frameForToolbarAtOrientation:[[UIApplication sharedApplication] statusBarOrientation]]];
  140. _toolbar.tintColor = [NCBrandColor sharedInstance].brand; //TWS
  141. _toolbar.barTintColor = [NCBrandColor sharedInstance].tabBar;
  142. [_toolbar setBackgroundImage:nil forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  143. [_toolbar setBackgroundImage:nil forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsCompact];
  144. _toolbar.barStyle = UIBarStyleDefault; //TWS
  145. _toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
  146. // Toolbar Items
  147. if (self.displayNavArrows) {
  148. NSString *arrowPathFormat = @"UIBarButtonItemArrow%@";
  149. UIImage *previousButtonImage = [UIImage imageForResourcePath:[NSString stringWithFormat:arrowPathFormat, @"Left"] ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]];
  150. UIImage *nextButtonImage = [UIImage imageForResourcePath:[NSString stringWithFormat:arrowPathFormat, @"Right"] ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]];
  151. _previousButton = [[UIBarButtonItem alloc] initWithImage:previousButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(gotoPreviousPage)];
  152. _nextButton = [[UIBarButtonItem alloc] initWithImage:nextButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(gotoNextPage)];
  153. }
  154. //TWS
  155. if (self.displayDeleteButton) {
  156. _deleteButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteButtonPressed:)];
  157. }
  158. if (self.displayActionButton) {
  159. _actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionButtonPressed:)];
  160. }
  161. if (self.displayShareButton) {
  162. _shareButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"actionSheetShare"] style:UIBarButtonItemStylePlain target:self action:@selector(shareButtonPressed:)];
  163. }
  164. // Update
  165. [self reloadData];
  166. // Swipe to dismiss
  167. if (_enableSwipeToDismiss) {
  168. UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(doneButtonPressed:)];
  169. swipeGesture.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;
  170. [self.view addGestureRecognizer:swipeGesture];
  171. }
  172. // Super
  173. [super viewDidLoad];
  174. }
  175. - (void)performLayout {
  176. // Setup
  177. _performingLayout = YES;
  178. NSUInteger numberOfPhotos = [self numberOfPhotos];
  179. // Setup pages
  180. [_visiblePages removeAllObjects];
  181. [_recycledPages removeAllObjects];
  182. // Navigation buttons
  183. if ([self.navigationController.viewControllers objectAtIndex:0] == self) {
  184. // We're first on stack so show done button
  185. _doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", nil) style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonPressed:)];
  186. // Set appearance
  187. [_doneButton setBackgroundImage:nil forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  188. [_doneButton setBackgroundImage:nil forState:UIControlStateNormal barMetrics:UIBarMetricsCompact];
  189. [_doneButton setBackgroundImage:nil forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  190. [_doneButton setBackgroundImage:nil forState:UIControlStateHighlighted barMetrics:UIBarMetricsCompact];
  191. [_doneButton setTitleTextAttributes:[NSDictionary dictionary] forState:UIControlStateNormal];
  192. [_doneButton setTitleTextAttributes:[NSDictionary dictionary] forState:UIControlStateHighlighted];
  193. self.navigationItem.rightBarButtonItem = _doneButton;
  194. }
  195. // color
  196. self.navigationController.navigationBar.barTintColor = [NCBrandColor sharedInstance].brand;
  197. self.navigationController.navigationBar.tintColor = [NCBrandColor sharedInstance].navigationBarText;
  198. [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [NCBrandColor sharedInstance].navigationBarText}];
  199. // Toolbar items
  200. BOOL hasItems = NO;
  201. UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
  202. fixedSpace.width = 32; // To balance action button
  203. UIBarButtonItem *fixedSpaceMini = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
  204. fixedSpaceMini.width = 25; // To balance action button
  205. UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
  206. NSMutableArray *items = [[NSMutableArray alloc] init];
  207. // Left button - Grid
  208. if (_enableGrid) {
  209. hasItems = YES;
  210. [items addObject:[[UIBarButtonItem alloc] initWithImage:[UIImage imageForResourcePath:@"UIBarButtonItemGrid" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]] style:UIBarButtonItemStylePlain target:self action:@selector(showGridAnimated)]];
  211. } else {
  212. [items addObject:fixedSpace];
  213. }
  214. // Middle - Nav
  215. if (_previousButton && _nextButton && numberOfPhotos > 1) {
  216. hasItems = YES;
  217. [items addObject:flexSpace];
  218. [items addObject:_previousButton];
  219. [items addObject:flexSpace];
  220. [items addObject:_nextButton];
  221. [items addObject:flexSpace];
  222. } else {
  223. [items addObject:flexSpace];
  224. }
  225. // Right - Action
  226. if (_actionButton && !(!hasItems && !self.navigationItem.rightBarButtonItem)) {
  227. if (_deleteButton) {
  228. [items addObject:_deleteButton];
  229. [items addObject:fixedSpaceMini];
  230. }
  231. if (_shareButton) {
  232. [items addObject:_shareButton];
  233. [items addObject:fixedSpaceMini];
  234. }
  235. [items addObject:_actionButton];
  236. } else {
  237. // We're not showing the toolbar so try and show in top right
  238. if (_actionButton)
  239. self.navigationItem.rightBarButtonItem = _actionButton;
  240. [items addObject:fixedSpace];
  241. }
  242. // Toolbar visibility
  243. [_toolbar setItems:items];
  244. BOOL hideToolbar = YES;
  245. for (UIBarButtonItem* item in _toolbar.items) {
  246. if (item != fixedSpace && item != flexSpace) {
  247. hideToolbar = NO;
  248. break;
  249. }
  250. }
  251. if (hideToolbar) {
  252. [_toolbar removeFromSuperview];
  253. } else {
  254. [self.view addSubview:_toolbar];
  255. }
  256. // Update nav
  257. [self updateNavigation];
  258. // Content offset
  259. _pagingScrollView.contentOffset = [self contentOffsetForPageAtIndex:_currentPageIndex];
  260. [self tilePages];
  261. _performingLayout = NO;
  262. }
  263. // Release any retained subviews of the main view.
  264. - (void)viewDidUnload {
  265. _currentPageIndex = 0;
  266. _pagingScrollView = nil;
  267. _visiblePages = nil;
  268. _recycledPages = nil;
  269. _toolbar = nil;
  270. _previousButton = nil;
  271. _nextButton = nil;
  272. _progressHUD = nil;
  273. [super viewDidUnload];
  274. }
  275. - (BOOL)presentingViewControllerPrefersStatusBarHidden {
  276. UIViewController *presenting = self.presentingViewController;
  277. if (presenting) {
  278. if ([presenting isKindOfClass:[UINavigationController class]]) {
  279. presenting = [(UINavigationController *)presenting topViewController];
  280. }
  281. } else {
  282. // We're in a navigation controller so get previous one!
  283. if (self.navigationController && self.navigationController.viewControllers.count > 1) {
  284. presenting = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2];
  285. }
  286. }
  287. if (presenting) {
  288. return [presenting prefersStatusBarHidden];
  289. } else {
  290. return NO;
  291. }
  292. }
  293. #pragma mark - Appearance
  294. - (void)viewWillAppear:(BOOL)animated {
  295. // Super
  296. [super viewWillAppear:animated];
  297. // Status bar
  298. if (!_viewHasAppearedInitially) {
  299. _leaveStatusBarAlone = [self presentingViewControllerPrefersStatusBarHidden];
  300. // Check if status bar is hidden on first appear, and if so then ignore it
  301. if (CGRectEqualToRect([[UIApplication sharedApplication] statusBarFrame], CGRectZero)) {
  302. _leaveStatusBarAlone = YES;
  303. }
  304. }
  305. // Nav Bar Appearance iPAD
  306. if (self.traitCollection.horizontalSizeClass != UIUserInterfaceSizeClassCompact) {
  307. // ----- TWS ----- //
  308. self.navigationItem.hidesBackButton = YES;
  309. self.navigationController.topViewController.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
  310. self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;
  311. }
  312. // Update UI
  313. [self hideControlsAfterDelay];
  314. // Initial appearance
  315. if (!_viewHasAppearedInitially) {
  316. if (_startOnGrid) {
  317. [self showGrid:NO];
  318. }
  319. }
  320. // If rotation occured while we're presenting a modal
  321. // and the index changed, make sure we show the right one now
  322. if (_currentPageIndex != _pageIndexBeforeRotation) {
  323. [self jumpToPageAtIndex:_pageIndexBeforeRotation animated:NO];
  324. }
  325. // Layaout
  326. [self.view setNeedsLayout];
  327. }
  328. - (void)viewDidAppear:(BOOL)animated {
  329. [super viewDidAppear:animated];
  330. _viewIsActive = YES;
  331. // Autoplay if first is video
  332. if (!_viewHasAppearedInitially) {
  333. if (_autoPlayOnAppear) {
  334. MWPhoto *photo = [self photoAtIndex:_currentPageIndex];
  335. if ([photo respondsToSelector:@selector(isVideo)] && photo.isVideo) {
  336. [self playVideoAtIndex:_currentPageIndex];
  337. }
  338. }
  339. }
  340. _viewHasAppearedInitially = YES;
  341. }
  342. - (void)viewWillDisappear:(BOOL)animated {
  343. // Detect if rotation occurs while we're presenting a modal
  344. _pageIndexBeforeRotation = _currentPageIndex;
  345. // Check that we're being popped for good
  346. if ([self.navigationController.viewControllers objectAtIndex:0] != self &&
  347. ![self.navigationController.viewControllers containsObject:self]) {
  348. // State
  349. _viewIsActive = NO;
  350. }
  351. // Controls
  352. [self.navigationController.navigationBar.layer removeAllAnimations]; // Stop all animations on nav bar
  353. [NSObject cancelPreviousPerformRequestsWithTarget:self]; // Cancel any pending toggles from taps
  354. [self setControlsHidden:NO animated:NO permanent:YES];
  355. // Super
  356. [super viewWillDisappear:animated];
  357. }
  358. - (void)willMoveToParentViewController:(UIViewController *)parent {
  359. if (parent && _hasBelongedToViewController) {
  360. [NSException raise:@"MWPhotoBrowser Instance Reuse" format:@"MWPhotoBrowser instances cannot be reused."];
  361. }
  362. }
  363. - (void)didMoveToParentViewController:(UIViewController *)parent {
  364. if (!parent) _hasBelongedToViewController = YES;
  365. }
  366. #pragma mark - Layout
  367. - (void)viewWillLayoutSubviews {
  368. [super viewWillLayoutSubviews];
  369. [self layoutVisiblePages];
  370. }
  371. - (void)layoutVisiblePages {
  372. // Flag
  373. _performingLayout = YES;
  374. // Toolbar
  375. _toolbar.frame = [self frameForToolbarAtOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
  376. // Remember index
  377. NSUInteger indexPriorToLayout = _currentPageIndex;
  378. // Get paging scroll view frame to determine if anything needs changing
  379. CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
  380. // Frame needs changing
  381. if (!_skipNextPagingScrollViewPositioning) {
  382. _pagingScrollView.frame = pagingScrollViewFrame;
  383. }
  384. _skipNextPagingScrollViewPositioning = NO;
  385. // Recalculate contentSize based on current orientation
  386. _pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
  387. // Adjust frames and configuration of each visible page
  388. for (MWZoomingScrollView *page in _visiblePages) {
  389. NSUInteger index = page.index;
  390. page.frame = [self frameForPageAtIndex:index];
  391. if (page.captionView) {
  392. page.captionView.frame = [self frameForCaptionView:page.captionView atIndex:index];
  393. }
  394. if (page.selectedButton) {
  395. page.selectedButton.frame = [self frameForSelectedButton:page.selectedButton atIndex:index];
  396. }
  397. if (page.playButton) {
  398. page.playButton.frame = [self frameForPlayButton:page.playButton atIndex:index];
  399. }
  400. // Adjust scales if bounds has changed since last time
  401. if (!CGRectEqualToRect(_previousLayoutBounds, self.view.bounds)) {
  402. // Update zooms for new bounds
  403. [page setMaxMinZoomScalesForCurrentBounds];
  404. _previousLayoutBounds = self.view.bounds;
  405. }
  406. }
  407. // Adjust video loading indicator if it's visible
  408. [self positionVideoLoadingIndicator];
  409. // Adjust contentOffset to preserve page location based on values collected prior to location
  410. _pagingScrollView.contentOffset = [self contentOffsetForPageAtIndex:indexPriorToLayout];
  411. [self didStartViewingPageAtIndex:_currentPageIndex]; // initial
  412. // Reset
  413. _currentPageIndex = indexPriorToLayout;
  414. _performingLayout = NO;
  415. }
  416. #pragma mark - Rotation
  417. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
  418. return YES;
  419. }
  420. #if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
  421. - (NSUInteger)supportedInterfaceOrientations
  422. #else
  423. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  424. #endif
  425. {
  426. return UIInterfaceOrientationMaskPortrait;
  427. }
  428. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  429. // Remember page index before rotation
  430. _pageIndexBeforeRotation = _currentPageIndex;
  431. _rotating = YES;
  432. // In iOS 7 the nav bar gets shown after rotation, but might as well do this for everything!
  433. if ([self areControlsHidden]) {
  434. // Force hidden
  435. self.navigationController.navigationBarHidden = YES;
  436. }
  437. }
  438. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  439. // Perform layout
  440. _currentPageIndex = _pageIndexBeforeRotation;
  441. // Delay control holding
  442. [self hideControlsAfterDelay];
  443. // Layout
  444. [self layoutVisiblePages];
  445. }
  446. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
  447. _rotating = NO;
  448. // Ensure nav bar isn't re-displayed
  449. if ([self areControlsHidden]) {
  450. self.navigationController.navigationBarHidden = NO;
  451. self.navigationController.navigationBar.alpha = 0;
  452. }
  453. }
  454. #pragma mark - Data
  455. - (NSUInteger)currentIndex {
  456. return _currentPageIndex;
  457. }
  458. - (void)reloadData {
  459. // Reset
  460. _photoCount = NSNotFound;
  461. // Get data
  462. NSUInteger numberOfPhotos = [self numberOfPhotos];
  463. [self releaseAllUnderlyingPhotos:YES];
  464. [_photos removeAllObjects];
  465. [_thumbPhotos removeAllObjects];
  466. for (int i = 0; i < numberOfPhotos; i++) {
  467. [_photos addObject:[NSNull null]];
  468. [_thumbPhotos addObject:[NSNull null]];
  469. }
  470. // Update current page index
  471. if (numberOfPhotos > 0) {
  472. _currentPageIndex = MAX(0, MIN(_currentPageIndex, numberOfPhotos - 1));
  473. } else {
  474. _currentPageIndex = 0;
  475. }
  476. // Update layout
  477. if ([self isViewLoaded]) {
  478. while (_pagingScrollView.subviews.count) {
  479. [[_pagingScrollView.subviews lastObject] removeFromSuperview];
  480. }
  481. [self performLayout];
  482. [self.view setNeedsLayout];
  483. }
  484. _toolbar.hidden = (_gridController ? true : false);
  485. _toolbar.alpha = (_gridController ? 0 : 1);
  486. //TWS
  487. if (!_gridController) {
  488. self.navigationController.navigationBar.hidden = (_gridController ? true : false);
  489. self.navigationController.navigationBar.alpha = (_gridController ? 0 : 1);
  490. }
  491. //TWS Update grid if it's presented
  492. [_gridController.collectionView reloadData];
  493. }
  494. - (void)reloadDataGridAtIndex:(NSArray *)indexPaths
  495. {
  496. [_gridController.collectionView reloadItemsAtIndexPaths:indexPaths];
  497. }
  498. //TWS
  499. - (BOOL)isGridReload:(NSUInteger)index
  500. {
  501. if (_gridController) {
  502. return [_gridController visibleGridIndexPath:index];
  503. } else return NO;
  504. }
  505. - (NSUInteger)numberOfPhotos {
  506. if (_photoCount == NSNotFound) {
  507. if ([_delegate respondsToSelector:@selector(numberOfPhotosInPhotoBrowser:)]) {
  508. _photoCount = [_delegate numberOfPhotosInPhotoBrowser:self];
  509. } else if (_fixedPhotosArray) {
  510. _photoCount = _fixedPhotosArray.count;
  511. }
  512. }
  513. if (_photoCount == NSNotFound) _photoCount = 0;
  514. return _photoCount;
  515. }
  516. - (id<MWPhoto>)photoAtIndex:(NSUInteger)index {
  517. id <MWPhoto> photo = nil;
  518. if (index < _photos.count) {
  519. if ([_photos objectAtIndex:index] == [NSNull null]) {
  520. if ([_delegate respondsToSelector:@selector(photoBrowser:photoAtIndex:)]) {
  521. photo = [_delegate photoBrowser:self photoAtIndex:index];
  522. } else if (_fixedPhotosArray && index < _fixedPhotosArray.count) {
  523. photo = [_fixedPhotosArray objectAtIndex:index];
  524. }
  525. if (photo) [_photos replaceObjectAtIndex:index withObject:photo];
  526. } else {
  527. photo = [_photos objectAtIndex:index];
  528. }
  529. }
  530. return photo;
  531. }
  532. - (id<MWPhoto>)thumbPhotoAtIndex:(NSUInteger)index {
  533. id <MWPhoto> photo = nil;
  534. if (index < _thumbPhotos.count) {
  535. if ([_thumbPhotos objectAtIndex:index] == [NSNull null]) {
  536. if ([_delegate respondsToSelector:@selector(photoBrowser:thumbPhotoAtIndex:)]) {
  537. photo = [_delegate photoBrowser:self thumbPhotoAtIndex:index];
  538. }
  539. if (photo) [_thumbPhotos replaceObjectAtIndex:index withObject:photo];
  540. } else {
  541. photo = [_thumbPhotos objectAtIndex:index];
  542. }
  543. }
  544. return photo;
  545. }
  546. - (MWCaptionView *)captionViewForPhotoAtIndex:(NSUInteger)index {
  547. MWCaptionView *captionView = nil;
  548. if ([_delegate respondsToSelector:@selector(photoBrowser:captionViewForPhotoAtIndex:)]) {
  549. captionView = [_delegate photoBrowser:self captionViewForPhotoAtIndex:index];
  550. } else {
  551. id <MWPhoto> photo = [self photoAtIndex:index];
  552. if ([photo respondsToSelector:@selector(caption)]) {
  553. if ([photo caption]) captionView = [[MWCaptionView alloc] initWithPhoto:photo];
  554. }
  555. }
  556. captionView.alpha = [self areControlsHidden] ? 0 : 1; // Initial alpha
  557. return captionView;
  558. }
  559. - (BOOL)photoIsSelectedAtIndex:(NSUInteger)index {
  560. BOOL value = NO;
  561. if (_displaySelectionButtons) {
  562. if ([self.delegate respondsToSelector:@selector(photoBrowser:isPhotoSelectedAtIndex:)]) {
  563. value = [self.delegate photoBrowser:self isPhotoSelectedAtIndex:index];
  564. }
  565. }
  566. return value;
  567. }
  568. - (void)setPhotoSelected:(BOOL)selected atIndex:(NSUInteger)index {
  569. if (_displaySelectionButtons) {
  570. if ([self.delegate respondsToSelector:@selector(photoBrowser:photoAtIndex:selectedChanged:)]) {
  571. [self.delegate photoBrowser:self photoAtIndex:index selectedChanged:selected];
  572. }
  573. }
  574. }
  575. - (UIImage *)imageForPhoto:(id<MWPhoto>)photo {
  576. if (photo) {
  577. // Get image or obtain in background
  578. if ([photo underlyingImage]) {
  579. return [photo underlyingImage];
  580. } else {
  581. [photo loadUnderlyingImageAndNotify];
  582. }
  583. }
  584. return nil;
  585. }
  586. - (void)loadAdjacentPhotosIfNecessary:(id<MWPhoto>)photo
  587. {
  588. MWZoomingScrollView *page = [self pageDisplayingPhoto:photo];
  589. if (page) {
  590. // If page is current page then initiate loading of previous and next pages
  591. NSUInteger pageIndex = page.index;
  592. if (_currentPageIndex == pageIndex) {
  593. if (pageIndex > 0) {
  594. // Preload index - 1
  595. id <MWPhoto> photo = [self photoAtIndex:pageIndex-1];
  596. if (![photo underlyingImage]) {
  597. [photo loadUnderlyingImageAndNotify];
  598. MWLog(@"Pre-loading image at index %lu", (unsigned long)pageIndex-1);
  599. }
  600. }
  601. if (pageIndex < [self numberOfPhotos] - 1) {
  602. // Preload index + 1
  603. id <MWPhoto> photo = [self photoAtIndex:pageIndex+1];
  604. if (![photo underlyingImage]) {
  605. [photo loadUnderlyingImageAndNotify];
  606. MWLog(@"Pre-loading image at index %lu", (unsigned long)pageIndex+1);
  607. }
  608. }
  609. }
  610. }
  611. }
  612. #pragma mark - MWPhoto Loading Notification
  613. - (void)handleMWPhotoLoadingDidEndNotification:(NSNotification *)notification {
  614. id <MWPhoto> photo = [notification object];
  615. MWZoomingScrollView *page = [self pageDisplayingPhoto:photo];
  616. if (page) {
  617. if ([photo underlyingImage]) {
  618. // Successful load
  619. [page displayImage];
  620. [self loadAdjacentPhotosIfNecessary:photo];
  621. } else {
  622. // Failed to load
  623. [page displayImageFailure];
  624. }
  625. // Update nav
  626. [self updateNavigation];
  627. }
  628. }
  629. #pragma mark - Paging
  630. - (void)tilePages {
  631. // Calculate which pages should be visible
  632. // Ignore padding as paging bounces encroach on that
  633. // and lead to false page loads
  634. CGRect visibleBounds = _pagingScrollView.bounds;
  635. NSInteger iFirstIndex = (NSInteger)floorf((CGRectGetMinX(visibleBounds)+PADDING*2) / CGRectGetWidth(visibleBounds));
  636. NSInteger iLastIndex = (NSInteger)floorf((CGRectGetMaxX(visibleBounds)-PADDING*2-1) / CGRectGetWidth(visibleBounds));
  637. if (iFirstIndex < 0) iFirstIndex = 0;
  638. if (iFirstIndex > [self numberOfPhotos] - 1) iFirstIndex = [self numberOfPhotos] - 1;
  639. if (iLastIndex < 0) iLastIndex = 0;
  640. if (iLastIndex > [self numberOfPhotos] - 1) iLastIndex = [self numberOfPhotos] - 1;
  641. // Recycle no longer needed pages
  642. NSInteger pageIndex;
  643. for (MWZoomingScrollView *page in _visiblePages) {
  644. pageIndex = page.index;
  645. if (pageIndex < (NSUInteger)iFirstIndex || pageIndex > (NSUInteger)iLastIndex) {
  646. [_recycledPages addObject:page];
  647. [page.captionView removeFromSuperview];
  648. [page.selectedButton removeFromSuperview];
  649. [page.playButton removeFromSuperview];
  650. [page prepareForReuse];
  651. [page removeFromSuperview];
  652. MWLog(@"Removed page at index %lu", (unsigned long)pageIndex);
  653. }
  654. }
  655. [_visiblePages minusSet:_recycledPages];
  656. while (_recycledPages.count > 2) // Only keep 2 recycled pages
  657. [_recycledPages removeObject:[_recycledPages anyObject]];
  658. // Add missing pages
  659. for (NSUInteger index = (NSUInteger)iFirstIndex; index <= (NSUInteger)iLastIndex; index++) {
  660. if (![self isDisplayingPageForIndex:index]) {
  661. // Add new page
  662. MWZoomingScrollView *page = [self dequeueRecycledPage];
  663. if (!page) {
  664. page = [[MWZoomingScrollView alloc] initWithPhotoBrowser:self];
  665. }
  666. [_visiblePages addObject:page];
  667. [self configurePage:page forIndex:index];
  668. [_pagingScrollView addSubview:page];
  669. MWLog(@"Added page at index %lu", (unsigned long)index);
  670. // Add caption
  671. MWCaptionView *captionView = [self captionViewForPhotoAtIndex:index];
  672. if (captionView) {
  673. captionView.frame = [self frameForCaptionView:captionView atIndex:index];
  674. [_pagingScrollView addSubview:captionView];
  675. page.captionView = captionView;
  676. }
  677. // Add play button if needed
  678. if (page.displayingVideo) {
  679. UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
  680. [playButton setImage:[UIImage imageForResourcePath:@"PlayButtonOverlayLarge" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]] forState:UIControlStateNormal];
  681. [playButton setImage:[UIImage imageForResourcePath:@"PlayButtonOverlayLargeTap" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]] forState:UIControlStateHighlighted];
  682. [playButton addTarget:self action:@selector(playButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
  683. [playButton sizeToFit];
  684. playButton.frame = [self frameForPlayButton:playButton atIndex:index];
  685. [_pagingScrollView addSubview:playButton];
  686. page.playButton = playButton;
  687. }
  688. // Add selected button
  689. if (self.displaySelectionButtons) {
  690. UIButton *selectedButton = [UIButton buttonWithType:UIButtonTypeCustom];
  691. [selectedButton setImage:[UIImage imageForResourcePath:@"ImageSelectedOff" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]] forState:UIControlStateNormal];
  692. UIImage *selectedOnImage;
  693. if (self.customImageSelectedIconName) {
  694. selectedOnImage = [UIImage imageNamed:self.customImageSelectedIconName];
  695. } else {
  696. selectedOnImage = [UIImage imageForResourcePath:@"ImageSelectedOn" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]];
  697. }
  698. [selectedButton setImage:selectedOnImage forState:UIControlStateSelected];
  699. [selectedButton sizeToFit];
  700. selectedButton.adjustsImageWhenHighlighted = NO;
  701. [selectedButton addTarget:self action:@selector(selectedButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
  702. selectedButton.frame = [self frameForSelectedButton:selectedButton atIndex:index];
  703. [_pagingScrollView addSubview:selectedButton];
  704. page.selectedButton = selectedButton;
  705. selectedButton.selected = [self photoIsSelectedAtIndex:index];
  706. }
  707. }
  708. }
  709. }
  710. - (void)updateVisiblePageStates {
  711. NSSet *copy = [_visiblePages copy];
  712. for (MWZoomingScrollView *page in copy) {
  713. // Update selection
  714. page.selectedButton.selected = [self photoIsSelectedAtIndex:page.index];
  715. }
  716. }
  717. - (BOOL)isDisplayingPageForIndex:(NSUInteger)index {
  718. for (MWZoomingScrollView *page in _visiblePages)
  719. if (page.index == index) return YES;
  720. return NO;
  721. }
  722. - (MWZoomingScrollView *)pageDisplayedAtIndex:(NSUInteger)index {
  723. MWZoomingScrollView *thePage = nil;
  724. for (MWZoomingScrollView *page in _visiblePages) {
  725. if (page.index == index) {
  726. thePage = page; break;
  727. }
  728. }
  729. return thePage;
  730. }
  731. - (MWZoomingScrollView *)pageDisplayingPhoto:(id<MWPhoto>)photo {
  732. MWZoomingScrollView *thePage = nil;
  733. for (MWZoomingScrollView *page in _visiblePages) {
  734. if (page.photo == photo) {
  735. thePage = page; break;
  736. }
  737. }
  738. return thePage;
  739. }
  740. - (void)configurePage:(MWZoomingScrollView *)page forIndex:(NSUInteger)index {
  741. page.frame = [self frameForPageAtIndex:index];
  742. page.index = index;
  743. page.photo = [self photoAtIndex:index];
  744. }
  745. - (MWZoomingScrollView *)dequeueRecycledPage {
  746. MWZoomingScrollView *page = [_recycledPages anyObject];
  747. if (page) {
  748. [_recycledPages removeObject:page];
  749. }
  750. return page;
  751. }
  752. // Handle page changes
  753. - (void)didStartViewingPageAtIndex:(NSUInteger)index {
  754. // Handle 0 photos
  755. if (![self numberOfPhotos]) {
  756. // Show controls
  757. [self setControlsHidden:NO animated:YES permanent:YES];
  758. return;
  759. }
  760. // Handle video on page change
  761. if (!_rotating || index != _currentVideoIndex) {
  762. [self clearCurrentVideo];
  763. }
  764. // Release images further away than +/-1
  765. NSUInteger i;
  766. if (index > 0) {
  767. // Release anything < index - 1
  768. for (i = 0; i < index-1; i++) {
  769. id photo = [_photos objectAtIndex:i];
  770. if (photo != [NSNull null]) {
  771. [photo unloadUnderlyingImage];
  772. [_photos replaceObjectAtIndex:i withObject:[NSNull null]];
  773. MWLog(@"Released underlying image at index %lu", (unsigned long)i);
  774. }
  775. }
  776. }
  777. if (index < [self numberOfPhotos] - 1) {
  778. // Release anything > index + 1
  779. for (i = index + 2; i < _photos.count; i++) {
  780. id photo = [_photos objectAtIndex:i];
  781. if (photo != [NSNull null]) {
  782. [photo unloadUnderlyingImage];
  783. [_photos replaceObjectAtIndex:i withObject:[NSNull null]];
  784. MWLog(@"Released underlying image at index %lu", (unsigned long)i);
  785. }
  786. }
  787. }
  788. // Load adjacent images if needed and the photo is already
  789. // loaded. Also called after photo has been loaded in background
  790. id <MWPhoto> currentPhoto = [self photoAtIndex:index];
  791. if ([currentPhoto underlyingImage]) {
  792. // photo loaded so load ajacent now
  793. [self loadAdjacentPhotosIfNecessary:currentPhoto];
  794. }
  795. // Notify delegate
  796. if (index != _previousPageIndex) {
  797. if ([_delegate respondsToSelector:@selector(photoBrowser:didDisplayPhotoAtIndex:)])
  798. [_delegate photoBrowser:self didDisplayPhotoAtIndex:index];
  799. _previousPageIndex = index;
  800. } else {
  801. if ([_delegate respondsToSelector:@selector(photoBrowser:didDisplayPhotoAtIndex:)])
  802. [_delegate photoBrowser:self didDisplayPhotoAtIndex:index];
  803. }
  804. // Update nav
  805. [self updateNavigation];
  806. }
  807. #pragma mark - Frame Calculations
  808. - (CGRect)frameForPagingScrollView {
  809. CGRect frame = self.view.bounds;// [[UIScreen mainScreen] bounds];
  810. frame.origin.x -= PADDING;
  811. frame.size.width += (2 * PADDING);
  812. return CGRectIntegral(frame);
  813. }
  814. - (CGRect)frameForPageAtIndex:(NSUInteger)index {
  815. // We have to use our paging scroll view's bounds, not frame, to calculate the page placement. When the device is in
  816. // landscape orientation, the frame will still be in portrait because the pagingScrollView is the root view controller's
  817. // view, so its frame is in window coordinate space, which is never rotated. Its bounds, however, will be in landscape
  818. // because it has a rotation transform applied.
  819. CGRect bounds = _pagingScrollView.bounds;
  820. CGRect pageFrame = bounds;
  821. pageFrame.size.width -= (2 * PADDING);
  822. pageFrame.origin.x = (bounds.size.width * index) + PADDING;
  823. return CGRectIntegral(pageFrame);
  824. }
  825. - (CGSize)contentSizeForPagingScrollView {
  826. // We have to use the paging scroll view's bounds to calculate the contentSize, for the same reason outlined above.
  827. CGRect bounds = _pagingScrollView.bounds;
  828. return CGSizeMake(bounds.size.width * [self numberOfPhotos], bounds.size.height);
  829. }
  830. - (CGPoint)contentOffsetForPageAtIndex:(NSUInteger)index {
  831. CGFloat pageWidth = _pagingScrollView.bounds.size.width;
  832. CGFloat newOffset = index * pageWidth;
  833. return CGPointMake(newOffset, 0);
  834. }
  835. - (CGRect)frameForToolbarAtOrientation:(UIInterfaceOrientation)orientation {
  836. CGFloat height = 49; //TWS
  837. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone &&
  838. UIInterfaceOrientationIsLandscape(orientation)) height = 49; //32
  839. return CGRectIntegral(CGRectMake(0, self.view.bounds.size.height - height, self.view.bounds.size.width, height));
  840. }
  841. - (CGRect)frameForCaptionView:(MWCaptionView *)captionView atIndex:(NSUInteger)index {
  842. CGRect pageFrame = [self frameForPageAtIndex:index];
  843. CGSize captionSize = [captionView sizeThatFits:CGSizeMake(pageFrame.size.width, 0)];
  844. CGRect captionFrame = CGRectMake(pageFrame.origin.x,
  845. pageFrame.size.height - captionSize.height - (_toolbar.superview?_toolbar.frame.size.height:0),
  846. pageFrame.size.width,
  847. captionSize.height);
  848. return CGRectIntegral(captionFrame);
  849. }
  850. - (CGRect)frameForSelectedButton:(UIButton *)selectedButton atIndex:(NSUInteger)index {
  851. CGRect pageFrame = [self frameForPageAtIndex:index];
  852. CGFloat padding = 20;
  853. CGFloat yOffset = 0;
  854. if (![self areControlsHidden]) {
  855. UINavigationBar *navBar = self.navigationController.navigationBar;
  856. yOffset = navBar.frame.origin.y + navBar.frame.size.height;
  857. }
  858. CGRect selectedButtonFrame = CGRectMake(pageFrame.origin.x + pageFrame.size.width - selectedButton.frame.size.width - padding,
  859. padding + yOffset,
  860. selectedButton.frame.size.width,
  861. selectedButton.frame.size.height);
  862. return CGRectIntegral(selectedButtonFrame);
  863. }
  864. - (CGRect)frameForPlayButton:(UIButton *)playButton atIndex:(NSUInteger)index {
  865. CGRect pageFrame = [self frameForPageAtIndex:index];
  866. return CGRectMake(floorf(CGRectGetMidX(pageFrame) - playButton.frame.size.width / 2),
  867. floorf(CGRectGetMidY(pageFrame) - playButton.frame.size.height / 2),
  868. playButton.frame.size.width,
  869. playButton.frame.size.height);
  870. }
  871. #pragma mark - UIScrollView Delegate
  872. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  873. // Checks
  874. if (!_viewIsActive || _performingLayout || _rotating) return;
  875. // Tile pages
  876. [self tilePages];
  877. // Calculate current page
  878. CGRect visibleBounds = _pagingScrollView.bounds;
  879. NSInteger index = (NSInteger)(floorf(CGRectGetMidX(visibleBounds) / CGRectGetWidth(visibleBounds)));
  880. if (index < 0) index = 0;
  881. if (index > [self numberOfPhotos] - 1) index = [self numberOfPhotos] - 1;
  882. NSUInteger previousCurrentPage = _currentPageIndex;
  883. _currentPageIndex = index;
  884. if (_currentPageIndex != previousCurrentPage) {
  885. [self didStartViewingPageAtIndex:index];
  886. }
  887. }
  888. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  889. // Hide controls when dragging begins
  890. [self setControlsHidden:YES animated:YES permanent:NO];
  891. }
  892. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  893. // Update nav when page changes
  894. [self updateNavigation];
  895. }
  896. #pragma mark - Navigation
  897. - (void)updateNavigation {
  898. // Title
  899. NSUInteger numberOfPhotos = [self numberOfPhotos];
  900. if (_gridController) {
  901. if (_gridController.selectionMode) {
  902. self.title = NSLocalizedString(@"Select Photos", nil);
  903. } else {
  904. NSString *photosText;
  905. if (numberOfPhotos == 1) {
  906. photosText = NSLocalizedString(@"photo", @"Used in the context: '1 photo'");
  907. } else {
  908. photosText = NSLocalizedString(@"photos", @"Used in the context: '3 photos'");
  909. }
  910. self.title = [NSString stringWithFormat:@"%lu %@", (unsigned long)numberOfPhotos, photosText];
  911. }
  912. } else if (numberOfPhotos > 1) {
  913. if ([_delegate respondsToSelector:@selector(photoBrowser:titleForPhotoAtIndex:)]) {
  914. self.title = [_delegate photoBrowser:self titleForPhotoAtIndex:_currentPageIndex];
  915. } else {
  916. self.title = [NSString stringWithFormat:@"%lu %@ %lu", (unsigned long)(_currentPageIndex+1), NSLocalizedString(@"of", @"Used in the context: 'Showing 1 of 3 items'"), (unsigned long)numberOfPhotos];
  917. }
  918. } else {
  919. self.title = nil;
  920. }
  921. // Buttons
  922. _previousButton.enabled = (_currentPageIndex > 0);
  923. _nextButton.enabled = (_currentPageIndex < numberOfPhotos - 1);
  924. //TWS Disable action button if there is no image or it's a video
  925. /*
  926. MWPhoto *photo = [self photoAtIndex:_currentPageIndex];
  927. if ([photo underlyingImage] == nil || ([photo respondsToSelector:@selector(isVideo)] && photo.isVideo)) {
  928. _actionButton.enabled = NO;
  929. _actionButton.tintColor = [UIColor clearColor]; // Tint to hide button
  930. } else {
  931. _actionButton.enabled = YES;
  932. _actionButton.tintColor = nil;
  933. }
  934. */
  935. }
  936. - (void)jumpToPageAtIndex:(NSUInteger)index animated:(BOOL)animated {
  937. // Change page
  938. if (index < [self numberOfPhotos]) {
  939. CGRect pageFrame = [self frameForPageAtIndex:index];
  940. [_pagingScrollView setContentOffset:CGPointMake(pageFrame.origin.x - PADDING, 0) animated:animated];
  941. [self updateNavigation];
  942. }
  943. // Update timer to give more time
  944. [self hideControlsAfterDelay];
  945. }
  946. - (void)gotoPreviousPage {
  947. [self showPreviousPhotoAnimated:NO];
  948. }
  949. - (void)gotoNextPage {
  950. [self showNextPhotoAnimated:NO];
  951. }
  952. - (void)showPreviousPhotoAnimated:(BOOL)animated {
  953. [self jumpToPageAtIndex:_currentPageIndex-1 animated:animated];
  954. }
  955. - (void)showNextPhotoAnimated:(BOOL)animated {
  956. [self jumpToPageAtIndex:_currentPageIndex+1 animated:animated];
  957. }
  958. #pragma mark - Interactions
  959. - (void)selectedButtonTapped:(id)sender {
  960. UIButton *selectedButton = (UIButton *)sender;
  961. selectedButton.selected = !selectedButton.selected;
  962. NSUInteger index = NSUIntegerMax;
  963. for (MWZoomingScrollView *page in _visiblePages) {
  964. if (page.selectedButton == selectedButton) {
  965. index = page.index;
  966. break;
  967. }
  968. }
  969. if (index != NSUIntegerMax) {
  970. [self setPhotoSelected:selectedButton.selected atIndex:index];
  971. }
  972. }
  973. - (void)playButtonTapped:(id)sender {
  974. UIButton *playButton = (UIButton *)sender;
  975. NSUInteger index = NSUIntegerMax;
  976. for (MWZoomingScrollView *page in _visiblePages) {
  977. if (page.playButton == playButton) {
  978. index = page.index;
  979. break;
  980. }
  981. }
  982. if (index != NSUIntegerMax) {
  983. if (!_currentVideoPlayerViewController) {
  984. [self playVideoAtIndex:index];
  985. }
  986. }
  987. }
  988. #pragma mark - Video
  989. - (void)playVideoAtIndex:(NSUInteger)index {
  990. id photo = [self photoAtIndex:index];
  991. if ([photo respondsToSelector:@selector(getVideoURL:)]) {
  992. // Valid for playing
  993. _currentVideoIndex = index;
  994. [self clearCurrentVideo];
  995. [self setVideoLoadingIndicatorVisible:YES atPageIndex:index];
  996. // Get video and play
  997. [photo getVideoURL:^(NSURL *url) {
  998. if (url) {
  999. dispatch_async(dispatch_get_main_queue(), ^{
  1000. [self _playVideo:url atPhotoIndex:index];
  1001. });
  1002. } else {
  1003. [self setVideoLoadingIndicatorVisible:NO atPageIndex:index];
  1004. }
  1005. }];
  1006. }
  1007. }
  1008. - (void)_playVideo:(NSURL *)videoURL atPhotoIndex:(NSUInteger)index {
  1009. // Setup player
  1010. _currentVideoPlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
  1011. [_currentVideoPlayerViewController.moviePlayer prepareToPlay];
  1012. _currentVideoPlayerViewController.moviePlayer.shouldAutoplay = YES;
  1013. _currentVideoPlayerViewController.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
  1014. _currentVideoPlayerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  1015. // Remove the movie player view controller from the "playback did finish" notification observers
  1016. // Observe ourselves so we can get it to use the crossfade transition
  1017. [[NSNotificationCenter defaultCenter] removeObserver:_currentVideoPlayerViewController
  1018. name:MPMoviePlayerPlaybackDidFinishNotification
  1019. object:_currentVideoPlayerViewController.moviePlayer];
  1020. [[NSNotificationCenter defaultCenter] addObserver:self
  1021. selector:@selector(videoFinishedCallback:)
  1022. name:MPMoviePlayerPlaybackDidFinishNotification
  1023. object:_currentVideoPlayerViewController.moviePlayer];
  1024. // Show
  1025. [self presentViewController:_currentVideoPlayerViewController animated:YES completion:nil];
  1026. }
  1027. - (void)videoFinishedCallback:(NSNotification*)notification {
  1028. // Remove observer
  1029. [[NSNotificationCenter defaultCenter] removeObserver:self
  1030. name:MPMoviePlayerPlaybackDidFinishNotification
  1031. object:_currentVideoPlayerViewController.moviePlayer];
  1032. // Clear up
  1033. [self clearCurrentVideo];
  1034. //TWS
  1035. [[NSNotificationCenter defaultCenter] postNotificationName:@"closePhotoBrowser" object:nil];
  1036. // Dismiss
  1037. BOOL error = [[[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue] == MPMovieFinishReasonPlaybackError;
  1038. if (error) {
  1039. // Error occured so dismiss with a delay incase error was immediate and we need to wait to dismiss the VC
  1040. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1041. [self dismissViewControllerAnimated:YES completion:nil];
  1042. });
  1043. } else {
  1044. [self dismissViewControllerAnimated:YES completion:nil];
  1045. }
  1046. }
  1047. - (void)clearCurrentVideo {
  1048. if (!_currentVideoPlayerViewController) return;
  1049. [_currentVideoLoadingIndicator removeFromSuperview];
  1050. _currentVideoPlayerViewController = nil;
  1051. _currentVideoLoadingIndicator = nil;
  1052. _currentVideoIndex = NSUIntegerMax;
  1053. }
  1054. - (void)setVideoLoadingIndicatorVisible:(BOOL)visible atPageIndex:(NSUInteger)pageIndex {
  1055. if (_currentVideoLoadingIndicator && !visible) {
  1056. [_currentVideoLoadingIndicator removeFromSuperview];
  1057. _currentVideoLoadingIndicator = nil;
  1058. } else if (!_currentVideoLoadingIndicator && visible) {
  1059. _currentVideoLoadingIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectZero];
  1060. [_currentVideoLoadingIndicator sizeToFit];
  1061. [_currentVideoLoadingIndicator startAnimating];
  1062. [_pagingScrollView addSubview:_currentVideoLoadingIndicator];
  1063. [self positionVideoLoadingIndicator];
  1064. }
  1065. }
  1066. - (void)positionVideoLoadingIndicator {
  1067. if (_currentVideoLoadingIndicator && _currentVideoIndex != NSUIntegerMax) {
  1068. CGRect frame = [self frameForPageAtIndex:_currentVideoIndex];
  1069. _currentVideoLoadingIndicator.center = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame));
  1070. }
  1071. }
  1072. #pragma mark - Grid
  1073. - (BOOL)isGridController {
  1074. return (_gridController ? true : false);
  1075. }
  1076. - (void)showGridAnimated {
  1077. [self showGrid:YES];
  1078. }
  1079. - (void)showGrid:(BOOL)animated {
  1080. if (_gridController) return;
  1081. // Init grid controller
  1082. _gridController = [[MWGridViewController alloc] init];
  1083. _gridController.initialContentOffset = _currentGridContentOffset;
  1084. _gridController.browser = self;
  1085. _gridController.selectionMode = _displaySelectionButtons;
  1086. _gridController.view.frame = self.view.bounds;
  1087. _gridController.view.frame = CGRectOffset(_gridController.view.frame, 0, (self.startOnGrid ? -1 : 1) * self.view.bounds.size.height);
  1088. // Stop specific layout being triggered
  1089. _skipNextPagingScrollViewPositioning = YES;
  1090. // Add as a child view controller
  1091. [self addChildViewController:_gridController];
  1092. [self.view addSubview:_gridController.view];
  1093. // Perform any adjustments
  1094. [_gridController.view layoutIfNeeded];
  1095. [_gridController adjustOffsetsAsRequired];
  1096. // Hide action button on nav bar if it exists
  1097. if (self.navigationItem.rightBarButtonItem == _actionButton) {
  1098. _gridPreviousRightNavItem = _actionButton;
  1099. [self.navigationItem setRightBarButtonItem:nil animated:YES];
  1100. } else {
  1101. _gridPreviousRightNavItem = nil;
  1102. }
  1103. // Update
  1104. [self updateNavigation];
  1105. [self setControlsHidden:NO animated:YES permanent:YES];
  1106. // Animate grid in and photo scroller out
  1107. [_gridController willMoveToParentViewController:self];
  1108. [UIView animateWithDuration:animated ? 0.3 : 0 animations:^(void) {
  1109. _gridController.view.frame = self.view.bounds;
  1110. CGRect newPagingFrame = [self frameForPagingScrollView];
  1111. newPagingFrame = CGRectOffset(newPagingFrame, 0, (self.startOnGrid ? 1 : -1) * newPagingFrame.size.height);
  1112. _pagingScrollView.frame = newPagingFrame;
  1113. } completion:^(BOOL finished) {
  1114. [_gridController didMoveToParentViewController:self];
  1115. }];
  1116. //TWS
  1117. if ([_delegate respondsToSelector:@selector(gridWillAppear:)]) {
  1118. // Call delegate method and let them dismiss us
  1119. [_delegate gridWillAppear:self];
  1120. }
  1121. }
  1122. - (void)hideGrid {
  1123. if (!_gridController) return;
  1124. // Remember previous content offset
  1125. _currentGridContentOffset = _gridController.collectionView.contentOffset;
  1126. // Restore action button if it was removed
  1127. if (_gridPreviousRightNavItem == _actionButton && _actionButton) {
  1128. [self.navigationItem setRightBarButtonItem:_gridPreviousRightNavItem animated:YES];
  1129. }
  1130. // Position prior to hide animation
  1131. CGRect newPagingFrame = [self frameForPagingScrollView];
  1132. newPagingFrame = CGRectOffset(newPagingFrame, 0, (self.startOnGrid ? 1 : -1) * newPagingFrame.size.height);
  1133. _pagingScrollView.frame = newPagingFrame;
  1134. // Remember and remove controller now so things can detect a nil grid controller
  1135. MWGridViewController *tmpGridController = _gridController;
  1136. _gridController = nil;
  1137. // Update
  1138. [self updateNavigation];
  1139. [self updateVisiblePageStates];
  1140. // Animate, hide grid and show paging scroll view
  1141. [UIView animateWithDuration:0.3 animations:^{
  1142. tmpGridController.view.frame = CGRectOffset(self.view.bounds, 0, (self.startOnGrid ? -1 : 1) * self.view.bounds.size.height);
  1143. _pagingScrollView.frame = [self frameForPagingScrollView];
  1144. } completion:^(BOOL finished) {
  1145. [tmpGridController willMoveToParentViewController:nil];
  1146. [tmpGridController.view removeFromSuperview];
  1147. [tmpGridController removeFromParentViewController];
  1148. [self setControlsHidden:NO animated:YES permanent:NO]; // retrigger timer
  1149. }];
  1150. //TWS
  1151. if ([_delegate respondsToSelector:@selector(gridWillDisappear:)]) {
  1152. // Call delegate method and let them dismiss us
  1153. [_delegate gridWillDisappear:self];
  1154. }
  1155. }
  1156. #pragma mark - Control Hiding / Showing
  1157. // If permanent then we don't set timers to hide again
  1158. // Fades all controls on iOS 5 & 6, and iOS 7 controls slide and fade
  1159. - (void)setControlsHidden:(BOOL)hidden animated:(BOOL)animated permanent:(BOOL)permanent {
  1160. // Force visible
  1161. if (![self numberOfPhotos] || _gridController || _alwaysShowControls)
  1162. hidden = NO;
  1163. // Cancel any timers
  1164. [self cancelControlHiding];
  1165. // Animations & positions
  1166. CGFloat animatonOffset = 20;
  1167. CGFloat animationDuration = (animated ? 0.35 : 0);
  1168. // Status bar
  1169. if (!_leaveStatusBarAlone) {
  1170. // Hide status bar
  1171. if (!_isVCBasedStatusBarAppearance) {
  1172. //TWS Non-view controller based
  1173. //[[UIApplication sharedApplication] setStatusBarHidden:hidden withAnimation:animated ? UIStatusBarAnimationSlide : UIStatusBarAnimationNone];
  1174. } else {
  1175. // View controller based so animate away
  1176. _statusBarShouldBeHidden = hidden;
  1177. //TWS
  1178. //[UIView animateWithDuration:animationDuration animations:^(void) {
  1179. // [self setNeedsStatusBarAppearanceUpdate];
  1180. //} completion:^(BOOL finished) {}];
  1181. }
  1182. }
  1183. // Toolbar, nav bar and captions
  1184. // Pre-appear animation positions for sliding
  1185. if ([self areControlsHidden] && !hidden && animated) {
  1186. // Toolbar
  1187. _toolbar.frame = CGRectOffset([self frameForToolbarAtOrientation:[[UIApplication sharedApplication] statusBarOrientation]], 0, animatonOffset);
  1188. // Captions
  1189. for (MWZoomingScrollView *page in _visiblePages) {
  1190. if (page.captionView) {
  1191. MWCaptionView *v = page.captionView;
  1192. //TWS
  1193. id <MWPhoto> photo = [self photoAtIndex:self.currentIndex];
  1194. if (photo.caption) {
  1195. if ([photo caption]) v.label.text = photo.caption;
  1196. }
  1197. // Pass any index, all we're interested in is the Y
  1198. CGRect captionFrame = [self frameForCaptionView:v atIndex:0];
  1199. captionFrame.origin.x = v.frame.origin.x; // Reset X
  1200. v.frame = CGRectOffset(captionFrame, 0, animatonOffset);
  1201. }
  1202. }
  1203. }
  1204. if ([_delegate respondsToSelector:@selector(setControlsHidden:animated:permanent:)]) {
  1205. [_delegate setControlsHidden:hidden animated:animated permanent:permanent];
  1206. }
  1207. [UIView animateWithDuration:animationDuration animations:^(void) {
  1208. CGFloat alpha = hidden ? 0 : 1;
  1209. // Nav bar slides up on it's own on iOS 7+
  1210. [self.navigationController.navigationBar setAlpha:alpha];
  1211. // Toolbar
  1212. _toolbar.frame = [self frameForToolbarAtOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
  1213. if (hidden) _toolbar.frame = CGRectOffset(_toolbar.frame, 0, animatonOffset);
  1214. _toolbar.alpha = alpha;
  1215. // Captions
  1216. for (MWZoomingScrollView *page in _visiblePages) {
  1217. if (page.captionView) {
  1218. MWCaptionView *v = page.captionView;
  1219. // Pass any index, all we're interested in is the Y
  1220. CGRect captionFrame = [self frameForCaptionView:v atIndex:0];
  1221. captionFrame.origin.x = v.frame.origin.x; // Reset X
  1222. if (hidden) captionFrame = CGRectOffset(captionFrame, 0, animatonOffset);
  1223. v.frame = captionFrame;
  1224. v.alpha = alpha;
  1225. }
  1226. }
  1227. // Selected buttons
  1228. for (MWZoomingScrollView *page in _visiblePages) {
  1229. if (page.selectedButton) {
  1230. UIButton *v = page.selectedButton;
  1231. CGRect newFrame = [self frameForSelectedButton:v atIndex:0];
  1232. newFrame.origin.x = v.frame.origin.x;
  1233. v.frame = newFrame;
  1234. }
  1235. }
  1236. } completion:^(BOOL finished) {}];
  1237. // Control hiding timer
  1238. // Will cancel existing timer but only begin hiding if
  1239. // they are visible
  1240. if (!permanent) [self hideControlsAfterDelay];
  1241. }
  1242. - (BOOL)prefersStatusBarHidden {
  1243. if (!_leaveStatusBarAlone) {
  1244. return _statusBarShouldBeHidden;
  1245. } else {
  1246. return [self presentingViewControllerPrefersStatusBarHidden];
  1247. }
  1248. }
  1249. - (UIStatusBarStyle)preferredStatusBarStyle {
  1250. return UIStatusBarStyleLightContent;
  1251. }
  1252. - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
  1253. return UIStatusBarAnimationSlide;
  1254. }
  1255. - (void)cancelControlHiding {
  1256. // If a timer exists then cancel and release
  1257. if (_controlVisibilityTimer) {
  1258. [_controlVisibilityTimer invalidate];
  1259. _controlVisibilityTimer = nil;
  1260. }
  1261. }
  1262. // Enable/disable control visiblity timer
  1263. - (void)hideControlsAfterDelay {
  1264. if (![self areControlsHidden]) {
  1265. [self cancelControlHiding];
  1266. _controlVisibilityTimer = [NSTimer scheduledTimerWithTimeInterval:self.delayToHideElements target:self selector:@selector(hideControls) userInfo:nil repeats:NO];
  1267. }
  1268. }
  1269. - (BOOL)areControlsHidden {
  1270. return (_toolbar.alpha == 0);
  1271. }
  1272. - (void)hideControls { [self setControlsHidden:YES animated:YES permanent:NO]; }
  1273. - (void)showControls { [self setControlsHidden:NO animated:YES permanent:NO]; }
  1274. - (void)toggleControls { [self setControlsHidden:![self areControlsHidden] animated:YES permanent:NO]; }
  1275. #pragma mark - Properties
  1276. - (void)setCurrentPhotoIndex:(NSUInteger)index {
  1277. // Validate
  1278. NSUInteger photoCount = [self numberOfPhotos];
  1279. if (photoCount == 0) {
  1280. index = 0;
  1281. } else {
  1282. if (index >= photoCount)
  1283. index = [self numberOfPhotos]-1;
  1284. }
  1285. _currentPageIndex = index;
  1286. if ([self isViewLoaded]) {
  1287. [self jumpToPageAtIndex:index animated:NO];
  1288. if (!_viewIsActive)
  1289. [self tilePages]; // Force tiling if view is not visible
  1290. }
  1291. }
  1292. #pragma mark - Misc
  1293. - (void)doneButtonPressed:(id)sender {
  1294. // Only if we're modal and there's a done button
  1295. if (_doneButton) {
  1296. // See if we actually just want to show/hide grid
  1297. if (self.enableGrid) {
  1298. if (self.startOnGrid && !_gridController) {
  1299. [self showGrid:YES];
  1300. return;
  1301. } else if (!self.startOnGrid && _gridController) {
  1302. [self hideGrid];
  1303. return;
  1304. }
  1305. }
  1306. // Dismiss view controller
  1307. if ([_delegate respondsToSelector:@selector(photoBrowserDidFinishModalPresentation:)]) {
  1308. // Call delegate method and let them dismiss us
  1309. [_delegate photoBrowserDidFinishModalPresentation:self];
  1310. } else {
  1311. [self dismissViewControllerAnimated:YES completion:nil];
  1312. }
  1313. }
  1314. }
  1315. #pragma mark - Delete
  1316. - (void)deleteButtonPressed:(id)sender {
  1317. if ([self.delegate respondsToSelector:@selector(photoBrowser:deleteButtonPressedForPhotoAtIndex:deleteButton:)])
  1318. [self.delegate photoBrowser:self deleteButtonPressedForPhotoAtIndex:_currentPageIndex deleteButton:self.deleteButton];
  1319. }
  1320. #pragma mark - Share
  1321. - (void)shareButtonPressed:(id)sender {
  1322. if ([self.delegate respondsToSelector:@selector(photoBrowser:shareButtonPressedForPhotoAtIndex:)])
  1323. [self.delegate photoBrowser:self shareButtonPressedForPhotoAtIndex:_currentPageIndex];
  1324. }
  1325. #pragma mark - Actions
  1326. - (void)actionButtonPressed:(id)sender {
  1327. // Only react when image has loaded
  1328. id <MWPhoto> photo = [self photoAtIndex:_currentPageIndex];
  1329. if ([self numberOfPhotos] > 0 && [photo underlyingImage]) {
  1330. // If they have defined a delegate method then just message them
  1331. if ([self.delegate respondsToSelector:@selector(photoBrowser:actionButtonPressedForPhotoAtIndex:)]) {
  1332. // Let delegate handle things
  1333. [self.delegate photoBrowser:self actionButtonPressedForPhotoAtIndex:_currentPageIndex];
  1334. } else {
  1335. // Show activity view controller
  1336. NSMutableArray *items = [NSMutableArray arrayWithObject:[photo underlyingImage]];
  1337. if (photo.caption) {
  1338. [items addObject:photo.caption];
  1339. }
  1340. self.activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
  1341. // Show
  1342. typeof(self) __weak weakSelf = self;
  1343. [self.activityViewController setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
  1344. weakSelf.activityViewController = nil;
  1345. [weakSelf hideControlsAfterDelay];
  1346. }];
  1347. // iOS 8 - Set the Anchor Point for the popover
  1348. if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8")) {
  1349. self.activityViewController.popoverPresentationController.barButtonItem = _actionButton;
  1350. }
  1351. [self presentViewController:self.activityViewController animated:YES completion:nil];
  1352. }
  1353. // Keep controls hidden
  1354. [self setControlsHidden:NO animated:YES permanent:YES];
  1355. }
  1356. }
  1357. @end