MWPhotoBrowser.m 60 KB

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