12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658 |
- #import <QuartzCore/QuartzCore.h>
- #import "MWCommon.h"
- #import "MWPhotoBrowser.h"
- #import "MWPhotoBrowserPrivate.h"
- #import "UIImage+MWPhotoBrowser.h"
- #define PADDING 10
- #pragma clang diagnostic ignored "-Wundeclared-selector"
- static void * MWVideoPlayerObservation = &MWVideoPlayerObservation;
- @implementation MWPhotoBrowser
- #pragma mark - Init
- - (id)init {
- if ((self = [super init])) {
- [self _initialisation];
- }
- return self;
- }
- - (id)initWithDelegate:(id <MWPhotoBrowserDelegate>)delegate {
- if ((self = [self init])) {
- _delegate = delegate;
- }
- return self;
- }
- - (id)initWithPhotos:(NSArray *)photosArray {
- if ((self = [self init])) {
- _fixedPhotosArray = photosArray;
- }
- return self;
- }
- - (id)initWithCoder:(NSCoder *)decoder {
- if ((self = [super initWithCoder:decoder])) {
- [self _initialisation];
- }
- return self;
- }
- - (void)_initialisation {
-
-
- NSNumber *isVCBasedStatusBarAppearanceNum = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"];
- if (isVCBasedStatusBarAppearanceNum) {
- _isVCBasedStatusBarAppearance = isVCBasedStatusBarAppearanceNum.boolValue;
- } else {
- _isVCBasedStatusBarAppearance = YES;
- }
- self.hidesBottomBarWhenPushed = YES;
- _hasBelongedToViewController = NO;
- _photoCount = NSNotFound;
- _previousLayoutBounds = CGRectZero;
- _currentPageIndex = 0;
- _previousPageIndex = NSUIntegerMax;
- _displayActionButton = YES;
- _displayDeleteButton = YES;
- _displayNavArrows = NO;
- _zoomPhotosToFill = YES;
- _performingLayout = NO;
- _rotating = NO;
- _viewIsActive = NO;
- _enableGrid = YES;
- _startOnGrid = NO;
- _enableSwipeToDismiss = YES;
- _delayToHideElements = 5;
- _visiblePages = [[NSMutableSet alloc] init];
- _recycledPages = [[NSMutableSet alloc] init];
- _photos = [[NSMutableArray alloc] init];
- _thumbPhotos = [[NSMutableArray alloc] init];
- _currentGridContentOffset = CGPointMake(0, CGFLOAT_MAX);
- _didSavePreviousStateOfNavBar = NO;
- self.automaticallyAdjustsScrollViewInsets = NO;
-
-
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(handleMWPhotoLoadingDidEndNotification:)
- name:MWPHOTO_LOADING_DID_END_NOTIFICATION
- object:nil];
-
- }
- - (void)dealloc {
- [self clearCurrentVideo];
- _pagingScrollView.delegate = nil;
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- [self releaseAllUnderlyingPhotos:NO];
- }
- - (void)releaseAllUnderlyingPhotos:(BOOL)preserveCurrent {
-
-
- NSArray *copy = [_photos copy];
- for (id p in copy) {
- if (p != [NSNull null]) {
- if (preserveCurrent && p == [self photoAtIndex:self.currentIndex]) {
- continue;
- }
- [p unloadUnderlyingImage];
- }
- }
-
- copy = [_thumbPhotos copy];
- for (id p in copy) {
- if (p != [NSNull null]) {
- [p unloadUnderlyingImage];
- }
- }
- }
- - (void)didReceiveMemoryWarning {
-
- [self releaseAllUnderlyingPhotos:YES];
- [_recycledPages removeAllObjects];
-
-
- [super didReceiveMemoryWarning];
-
- }
- #pragma mark - View Loading
- - (void)viewDidLoad {
-
-
- if (_startOnGrid) _enableGrid = YES;
- if (_enableGrid) {
- _enableGrid = [_delegate respondsToSelector:@selector(photoBrowser:thumbPhotoAtIndex:)];
- }
- if (!_enableGrid) _startOnGrid = NO;
-
-
- self.view.backgroundColor = [UIColor whiteColor];
- self.view.clipsToBounds = YES;
-
-
- CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
- _pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
- _pagingScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- _pagingScrollView.pagingEnabled = YES;
- _pagingScrollView.delegate = self;
- _pagingScrollView.showsHorizontalScrollIndicator = NO;
- _pagingScrollView.showsVerticalScrollIndicator = NO;
- _pagingScrollView.backgroundColor = [UIColor whiteColor];
- _pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
- [self.view addSubview:_pagingScrollView];
-
-
- _toolbar = [[UIToolbar alloc] initWithFrame:[self frameForToolbarAtOrientation:[[UIApplication sharedApplication] statusBarOrientation]]];
- _toolbar.tintColor = COLOR_BRAND;
- _toolbar.barTintColor = nil;
- [_toolbar setBackgroundImage:nil forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
- [_toolbar setBackgroundImage:nil forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsCompact];
- _toolbar.barStyle = UIBarStyleDefault;
- _toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
-
-
- if (self.displayNavArrows) {
- NSString *arrowPathFormat = @"UIBarButtonItemArrow%@";
- UIImage *previousButtonImage = [UIImage imageForResourcePath:[NSString stringWithFormat:arrowPathFormat, @"Left"] ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]];
- UIImage *nextButtonImage = [UIImage imageForResourcePath:[NSString stringWithFormat:arrowPathFormat, @"Right"] ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]];
- _previousButton = [[UIBarButtonItem alloc] initWithImage:previousButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(gotoPreviousPage)];
- _nextButton = [[UIBarButtonItem alloc] initWithImage:nextButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(gotoNextPage)];
- }
-
-
- if (self.displayDeleteButton) {
- _deleteButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteButtonPressed:)];
- }
-
- if (self.displayActionButton) {
- _actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionButtonPressed:)];
- }
-
-
- [self reloadData];
-
-
- if (_enableSwipeToDismiss) {
- UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(doneButtonPressed:)];
- swipeGesture.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;
- [self.view addGestureRecognizer:swipeGesture];
- }
-
-
- [super viewDidLoad];
-
- }
- - (void)performLayout {
-
-
- _performingLayout = YES;
- NSUInteger numberOfPhotos = [self numberOfPhotos];
-
-
- [_visiblePages removeAllObjects];
- [_recycledPages removeAllObjects];
-
-
- if ([self.navigationController.viewControllers objectAtIndex:0] == self) {
-
- _doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", nil) style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonPressed:)];
-
- [_doneButton setBackgroundImage:nil forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
- [_doneButton setBackgroundImage:nil forState:UIControlStateNormal barMetrics:UIBarMetricsCompact];
- [_doneButton setBackgroundImage:nil forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
- [_doneButton setBackgroundImage:nil forState:UIControlStateHighlighted barMetrics:UIBarMetricsCompact];
- [_doneButton setTitleTextAttributes:[NSDictionary dictionary] forState:UIControlStateNormal];
- [_doneButton setTitleTextAttributes:[NSDictionary dictionary] forState:UIControlStateHighlighted];
- self.navigationItem.rightBarButtonItem = _doneButton;
- } else {
-
-
- }
-
-
- BOOL hasItems = NO;
- UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
- fixedSpace.width = 32;
- UIBarButtonItem *fixedSpaceMini = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
- fixedSpaceMini.width = 25;
- UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
- NSMutableArray *items = [[NSMutableArray alloc] init];
-
- if (_enableGrid) {
- hasItems = YES;
- [items addObject:[[UIBarButtonItem alloc] initWithImage:[UIImage imageForResourcePath:@"UIBarButtonItemGrid" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]] style:UIBarButtonItemStylePlain target:self action:@selector(showGridAnimated)]];
- } else {
- [items addObject:fixedSpace];
- }
-
- if (_previousButton && _nextButton && numberOfPhotos > 1) {
- hasItems = YES;
- [items addObject:flexSpace];
- [items addObject:_previousButton];
- [items addObject:flexSpace];
- [items addObject:_nextButton];
- [items addObject:flexSpace];
- } else {
- [items addObject:flexSpace];
- }
-
- if (_actionButton && !(!hasItems && !self.navigationItem.rightBarButtonItem)) {
-
- if (_deleteButton) {
-
- [items addObject:_deleteButton];
- [items addObject:fixedSpaceMini];
- }
- [items addObject:_actionButton];
-
- } else {
-
-
- if (_actionButton)
- self.navigationItem.rightBarButtonItem = _actionButton;
- [items addObject:fixedSpace];
- }
-
- [_toolbar setItems:items];
- BOOL hideToolbar = YES;
- for (UIBarButtonItem* item in _toolbar.items) {
- if (item != fixedSpace && item != flexSpace) {
- hideToolbar = NO;
- break;
- }
- }
- if (hideToolbar) {
- [_toolbar removeFromSuperview];
- } else {
- [self.view addSubview:_toolbar];
- }
-
-
- [self updateNavigation];
-
-
- _pagingScrollView.contentOffset = [self contentOffsetForPageAtIndex:_currentPageIndex];
- [self tilePages];
- _performingLayout = NO;
-
- }
- - (void)viewDidUnload {
- _currentPageIndex = 0;
- _pagingScrollView = nil;
- _visiblePages = nil;
- _recycledPages = nil;
- _toolbar = nil;
- _previousButton = nil;
- _nextButton = nil;
- _progressHUD = nil;
- [super viewDidUnload];
- }
- - (BOOL)presentingViewControllerPrefersStatusBarHidden {
- UIViewController *presenting = self.presentingViewController;
- if (presenting) {
- if ([presenting isKindOfClass:[UINavigationController class]]) {
- presenting = [(UINavigationController *)presenting topViewController];
- }
- } else {
-
- if (self.navigationController && self.navigationController.viewControllers.count > 1) {
- presenting = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2];
- }
- }
- if (presenting) {
- return [presenting prefersStatusBarHidden];
- } else {
- return NO;
- }
- }
- #pragma mark - Appearance
- - (void)viewWillAppear:(BOOL)animated {
-
-
- [super viewWillAppear:animated];
-
-
- if (!_viewHasAppearedInitially) {
- _leaveStatusBarAlone = [self presentingViewControllerPrefersStatusBarHidden];
-
- if (CGRectEqualToRect([[UIApplication sharedApplication] statusBarFrame], CGRectZero)) {
- _leaveStatusBarAlone = YES;
- }
- }
-
-
- if (self.traitCollection.horizontalSizeClass != UIUserInterfaceSizeClassCompact) {
-
-
- self.navigationItem.hidesBackButton = YES;
-
- self.navigationController.topViewController.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
- self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;
- }
-
- [self hideControlsAfterDelay];
-
-
- if (!_viewHasAppearedInitially) {
- if (_startOnGrid) {
- [self showGrid:NO];
- }
- }
-
-
-
- if (_currentPageIndex != _pageIndexBeforeRotation) {
- [self jumpToPageAtIndex:_pageIndexBeforeRotation animated:NO];
- }
-
- [self.view setNeedsLayout];
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
- _viewIsActive = YES;
-
-
- if (!_viewHasAppearedInitially) {
- if (_autoPlayOnAppear) {
- MWPhoto *photo = [self photoAtIndex:_currentPageIndex];
- if ([photo respondsToSelector:@selector(isVideo)] && photo.isVideo) {
- [self playVideoAtIndex:_currentPageIndex];
- }
- }
- }
-
- _viewHasAppearedInitially = YES;
-
- }
- - (void)viewWillDisappear:(BOOL)animated {
-
-
- _pageIndexBeforeRotation = _currentPageIndex;
-
-
- if ([self.navigationController.viewControllers objectAtIndex:0] != self &&
- ![self.navigationController.viewControllers containsObject:self]) {
-
-
- _viewIsActive = NO;
- }
-
-
- [self.navigationController.navigationBar.layer removeAllAnimations];
- [NSObject cancelPreviousPerformRequestsWithTarget:self];
- [self setControlsHidden:NO animated:NO permanent:YES];
-
-
- if (!_leaveStatusBarAlone && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
- [[UIApplication sharedApplication] setStatusBarStyle:_previousStatusBarStyle animated:animated];
- }
-
-
- [super viewWillDisappear:animated];
-
- }
- - (void)willMoveToParentViewController:(UIViewController *)parent {
- if (parent && _hasBelongedToViewController) {
- [NSException raise:@"MWPhotoBrowser Instance Reuse" format:@"MWPhotoBrowser instances cannot be reused."];
- }
- }
- - (void)didMoveToParentViewController:(UIViewController *)parent {
- if (!parent) _hasBelongedToViewController = YES;
- }
- #pragma mark - Layout
- - (void)viewWillLayoutSubviews {
- [super viewWillLayoutSubviews];
- [self layoutVisiblePages];
- }
- - (void)layoutVisiblePages {
-
-
- _performingLayout = YES;
-
-
- _toolbar.frame = [self frameForToolbarAtOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
-
-
- NSUInteger indexPriorToLayout = _currentPageIndex;
-
-
- CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
-
-
- if (!_skipNextPagingScrollViewPositioning) {
- _pagingScrollView.frame = pagingScrollViewFrame;
- }
- _skipNextPagingScrollViewPositioning = NO;
-
-
- _pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
-
-
- for (MWZoomingScrollView *page in _visiblePages) {
- NSUInteger index = page.index;
- page.frame = [self frameForPageAtIndex:index];
- if (page.captionView) {
- page.captionView.frame = [self frameForCaptionView:page.captionView atIndex:index];
- }
- if (page.selectedButton) {
- page.selectedButton.frame = [self frameForSelectedButton:page.selectedButton atIndex:index];
- }
- if (page.playButton) {
- page.playButton.frame = [self frameForPlayButton:page.playButton atIndex:index];
- }
-
-
- if (!CGRectEqualToRect(_previousLayoutBounds, self.view.bounds)) {
-
- [page setMaxMinZoomScalesForCurrentBounds];
- _previousLayoutBounds = self.view.bounds;
- }
- }
-
-
- [self positionVideoLoadingIndicator];
-
-
- _pagingScrollView.contentOffset = [self contentOffsetForPageAtIndex:indexPriorToLayout];
- [self didStartViewingPageAtIndex:_currentPageIndex];
-
-
- _currentPageIndex = indexPriorToLayout;
- _performingLayout = NO;
-
- }
- #pragma mark - Rotation
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
- return YES;
- }
- #if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- - (NSUInteger)supportedInterfaceOrientations
- #else
- - (UIInterfaceOrientationMask)supportedInterfaceOrientations
- #endif
- {
- return UIInterfaceOrientationMaskPortrait;
- }
- - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
-
-
- _pageIndexBeforeRotation = _currentPageIndex;
- _rotating = YES;
-
-
- if ([self areControlsHidden]) {
-
- self.navigationController.navigationBarHidden = YES;
- }
- }
- - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
-
-
- _currentPageIndex = _pageIndexBeforeRotation;
-
-
- [self hideControlsAfterDelay];
-
-
- [self layoutVisiblePages];
-
- }
- - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
- _rotating = NO;
-
- if ([self areControlsHidden]) {
- self.navigationController.navigationBarHidden = NO;
- self.navigationController.navigationBar.alpha = 0;
- }
- }
- #pragma mark - Data
- - (NSUInteger)currentIndex {
- return _currentPageIndex;
- }
- - (void)reloadData {
-
-
- _photoCount = NSNotFound;
-
-
- NSUInteger numberOfPhotos = [self numberOfPhotos];
- [self releaseAllUnderlyingPhotos:YES];
- [_photos removeAllObjects];
- [_thumbPhotos removeAllObjects];
- for (int i = 0; i < numberOfPhotos; i++) {
- [_photos addObject:[NSNull null]];
- [_thumbPhotos addObject:[NSNull null]];
- }
-
- if (numberOfPhotos > 0) {
- _currentPageIndex = MAX(0, MIN(_currentPageIndex, numberOfPhotos - 1));
- } else {
- _currentPageIndex = 0;
- }
-
-
- if ([self isViewLoaded]) {
- while (_pagingScrollView.subviews.count) {
- [[_pagingScrollView.subviews lastObject] removeFromSuperview];
- }
- [self performLayout];
- [self.view setNeedsLayout];
- }
- _toolbar.hidden = (_gridController ? true : false);
- _toolbar.alpha = (_gridController ? 0 : 1);
-
- if (!_gridController) {
- self.navigationController.navigationBar.hidden = (_gridController ? true : false);
- self.navigationController.navigationBar.alpha = (_gridController ? 0 : 1);
- }
-
-
- [_gridController.collectionView reloadData];
- }
- - (void)reloadDataGridAtIndex:(NSArray *)indexPaths
- {
- [_gridController.collectionView reloadItemsAtIndexPaths:indexPaths];
- }
- - (BOOL)isGridReload:(NSUInteger)index
- {
- if (_gridController) {
-
- return [_gridController visibleGridIndexPath:index];
-
- } else return NO;
- }
- - (NSUInteger)numberOfPhotos {
- if (_photoCount == NSNotFound) {
- if ([_delegate respondsToSelector:@selector(numberOfPhotosInPhotoBrowser:)]) {
- _photoCount = [_delegate numberOfPhotosInPhotoBrowser:self];
- } else if (_fixedPhotosArray) {
- _photoCount = _fixedPhotosArray.count;
- }
- }
- if (_photoCount == NSNotFound) _photoCount = 0;
- return _photoCount;
- }
- - (id<MWPhoto>)photoAtIndex:(NSUInteger)index {
- id <MWPhoto> photo = nil;
- if (index < _photos.count) {
- if ([_photos objectAtIndex:index] == [NSNull null]) {
- if ([_delegate respondsToSelector:@selector(photoBrowser:photoAtIndex:)]) {
- photo = [_delegate photoBrowser:self photoAtIndex:index];
- } else if (_fixedPhotosArray && index < _fixedPhotosArray.count) {
- photo = [_fixedPhotosArray objectAtIndex:index];
- }
- if (photo) [_photos replaceObjectAtIndex:index withObject:photo];
- } else {
- photo = [_photos objectAtIndex:index];
- }
- }
- return photo;
- }
- - (id<MWPhoto>)thumbPhotoAtIndex:(NSUInteger)index {
- id <MWPhoto> photo = nil;
- if (index < _thumbPhotos.count) {
- if ([_thumbPhotos objectAtIndex:index] == [NSNull null]) {
- if ([_delegate respondsToSelector:@selector(photoBrowser:thumbPhotoAtIndex:)]) {
- photo = [_delegate photoBrowser:self thumbPhotoAtIndex:index];
- }
- if (photo) [_thumbPhotos replaceObjectAtIndex:index withObject:photo];
- } else {
- photo = [_thumbPhotos objectAtIndex:index];
- }
- }
- return photo;
- }
- - (MWCaptionView *)captionViewForPhotoAtIndex:(NSUInteger)index {
- MWCaptionView *captionView = nil;
- if ([_delegate respondsToSelector:@selector(photoBrowser:captionViewForPhotoAtIndex:)]) {
- captionView = [_delegate photoBrowser:self captionViewForPhotoAtIndex:index];
- } else {
- id <MWPhoto> photo = [self photoAtIndex:index];
- if ([photo respondsToSelector:@selector(caption)]) {
- if ([photo caption]) captionView = [[MWCaptionView alloc] initWithPhoto:photo];
- }
- }
- captionView.alpha = [self areControlsHidden] ? 0 : 1;
- return captionView;
- }
- - (BOOL)photoIsSelectedAtIndex:(NSUInteger)index {
- BOOL value = NO;
- if (_displaySelectionButtons) {
- if ([self.delegate respondsToSelector:@selector(photoBrowser:isPhotoSelectedAtIndex:)]) {
- value = [self.delegate photoBrowser:self isPhotoSelectedAtIndex:index];
- }
- }
- return value;
- }
- - (void)setPhotoSelected:(BOOL)selected atIndex:(NSUInteger)index {
- if (_displaySelectionButtons) {
- if ([self.delegate respondsToSelector:@selector(photoBrowser:photoAtIndex:selectedChanged:)]) {
- [self.delegate photoBrowser:self photoAtIndex:index selectedChanged:selected];
- }
- }
- }
- - (UIImage *)imageForPhoto:(id<MWPhoto>)photo {
- if (photo) {
-
- if ([photo underlyingImage]) {
- return [photo underlyingImage];
- } else {
- [photo loadUnderlyingImageAndNotify];
- }
- }
- return nil;
- }
- - (void)loadAdjacentPhotosIfNecessary:(id<MWPhoto>)photo
- {
- MWZoomingScrollView *page = [self pageDisplayingPhoto:photo];
-
- if (page) {
-
- NSUInteger pageIndex = page.index;
- if (_currentPageIndex == pageIndex) {
-
- if (pageIndex > 0) {
-
-
- id <MWPhoto> photo = [self photoAtIndex:pageIndex-1];
-
- if (![photo underlyingImage]) {
- [photo loadUnderlyingImageAndNotify];
- MWLog(@"Pre-loading image at index %lu", (unsigned long)pageIndex-1);
- }
- }
-
- if (pageIndex < [self numberOfPhotos] - 1) {
-
-
- id <MWPhoto> photo = [self photoAtIndex:pageIndex+1];
-
- if (![photo underlyingImage]) {
- [photo loadUnderlyingImageAndNotify];
- MWLog(@"Pre-loading image at index %lu", (unsigned long)pageIndex+1);
- }
- }
- }
- }
-
- }
- #pragma mark - MWPhoto Loading Notification
- - (void)handleMWPhotoLoadingDidEndNotification:(NSNotification *)notification {
- id <MWPhoto> photo = [notification object];
- MWZoomingScrollView *page = [self pageDisplayingPhoto:photo];
- if (page) {
- if ([photo underlyingImage]) {
-
- [page displayImage];
- [self loadAdjacentPhotosIfNecessary:photo];
- } else {
-
-
- [page displayImageFailure];
- }
-
- [self updateNavigation];
- }
- }
- #pragma mark - Paging
- - (void)tilePages {
-
-
-
-
- CGRect visibleBounds = _pagingScrollView.bounds;
- NSInteger iFirstIndex = (NSInteger)floorf((CGRectGetMinX(visibleBounds)+PADDING*2) / CGRectGetWidth(visibleBounds));
- NSInteger iLastIndex = (NSInteger)floorf((CGRectGetMaxX(visibleBounds)-PADDING*2-1) / CGRectGetWidth(visibleBounds));
- if (iFirstIndex < 0) iFirstIndex = 0;
- if (iFirstIndex > [self numberOfPhotos] - 1) iFirstIndex = [self numberOfPhotos] - 1;
- if (iLastIndex < 0) iLastIndex = 0;
- if (iLastIndex > [self numberOfPhotos] - 1) iLastIndex = [self numberOfPhotos] - 1;
-
-
- NSInteger pageIndex;
- for (MWZoomingScrollView *page in _visiblePages) {
- pageIndex = page.index;
- if (pageIndex < (NSUInteger)iFirstIndex || pageIndex > (NSUInteger)iLastIndex) {
- [_recycledPages addObject:page];
- [page.captionView removeFromSuperview];
- [page.selectedButton removeFromSuperview];
- [page.playButton removeFromSuperview];
- [page prepareForReuse];
- [page removeFromSuperview];
- MWLog(@"Removed page at index %lu", (unsigned long)pageIndex);
- }
- }
- [_visiblePages minusSet:_recycledPages];
- while (_recycledPages.count > 2)
- [_recycledPages removeObject:[_recycledPages anyObject]];
-
-
- for (NSUInteger index = (NSUInteger)iFirstIndex; index <= (NSUInteger)iLastIndex; index++) {
- if (![self isDisplayingPageForIndex:index]) {
-
-
- MWZoomingScrollView *page = [self dequeueRecycledPage];
- if (!page) {
- page = [[MWZoomingScrollView alloc] initWithPhotoBrowser:self];
- }
- [_visiblePages addObject:page];
- [self configurePage:page forIndex:index];
- [_pagingScrollView addSubview:page];
- MWLog(@"Added page at index %lu", (unsigned long)index);
-
-
- MWCaptionView *captionView = [self captionViewForPhotoAtIndex:index];
- if (captionView) {
- captionView.frame = [self frameForCaptionView:captionView atIndex:index];
- [_pagingScrollView addSubview:captionView];
- page.captionView = captionView;
- }
-
-
- if (page.displayingVideo) {
- UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [playButton setImage:[UIImage imageForResourcePath:@"PlayButtonOverlayLarge" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]] forState:UIControlStateNormal];
- [playButton setImage:[UIImage imageForResourcePath:@"PlayButtonOverlayLargeTap" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]] forState:UIControlStateHighlighted];
- [playButton addTarget:self action:@selector(playButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
- [playButton sizeToFit];
- playButton.frame = [self frameForPlayButton:playButton atIndex:index];
- [_pagingScrollView addSubview:playButton];
- page.playButton = playButton;
- }
-
-
- if (self.displaySelectionButtons) {
- UIButton *selectedButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [selectedButton setImage:[UIImage imageForResourcePath:@"ImageSelectedOff" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]] forState:UIControlStateNormal];
- UIImage *selectedOnImage;
- if (self.customImageSelectedIconName) {
- selectedOnImage = [UIImage imageNamed:self.customImageSelectedIconName];
- } else {
- selectedOnImage = [UIImage imageForResourcePath:@"ImageSelectedOn" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]];
- }
- [selectedButton setImage:selectedOnImage forState:UIControlStateSelected];
- [selectedButton sizeToFit];
- selectedButton.adjustsImageWhenHighlighted = NO;
- [selectedButton addTarget:self action:@selector(selectedButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
- selectedButton.frame = [self frameForSelectedButton:selectedButton atIndex:index];
- [_pagingScrollView addSubview:selectedButton];
- page.selectedButton = selectedButton;
- selectedButton.selected = [self photoIsSelectedAtIndex:index];
- }
-
- }
- }
-
- }
- - (void)updateVisiblePageStates {
- NSSet *copy = [_visiblePages copy];
- for (MWZoomingScrollView *page in copy) {
-
-
- page.selectedButton.selected = [self photoIsSelectedAtIndex:page.index];
-
- }
- }
- - (BOOL)isDisplayingPageForIndex:(NSUInteger)index {
- for (MWZoomingScrollView *page in _visiblePages)
- if (page.index == index) return YES;
- return NO;
- }
- - (MWZoomingScrollView *)pageDisplayedAtIndex:(NSUInteger)index {
- MWZoomingScrollView *thePage = nil;
- for (MWZoomingScrollView *page in _visiblePages) {
- if (page.index == index) {
- thePage = page; break;
- }
- }
- return thePage;
- }
- - (MWZoomingScrollView *)pageDisplayingPhoto:(id<MWPhoto>)photo {
- MWZoomingScrollView *thePage = nil;
- for (MWZoomingScrollView *page in _visiblePages) {
- if (page.photo == photo) {
- thePage = page; break;
- }
- }
- return thePage;
- }
- - (void)configurePage:(MWZoomingScrollView *)page forIndex:(NSUInteger)index {
- page.frame = [self frameForPageAtIndex:index];
- page.index = index;
- page.photo = [self photoAtIndex:index];
- }
- - (MWZoomingScrollView *)dequeueRecycledPage {
- MWZoomingScrollView *page = [_recycledPages anyObject];
- if (page) {
- [_recycledPages removeObject:page];
- }
- return page;
- }
- - (void)didStartViewingPageAtIndex:(NSUInteger)index {
-
-
- if (![self numberOfPhotos]) {
-
- [self setControlsHidden:NO animated:YES permanent:YES];
- return;
- }
-
-
- if (!_rotating || index != _currentVideoIndex) {
- [self clearCurrentVideo];
- }
-
-
- NSUInteger i;
- if (index > 0) {
-
- for (i = 0; i < index-1; i++) {
- id photo = [_photos objectAtIndex:i];
- if (photo != [NSNull null]) {
- [photo unloadUnderlyingImage];
- [_photos replaceObjectAtIndex:i withObject:[NSNull null]];
- MWLog(@"Released underlying image at index %lu", (unsigned long)i);
- }
- }
- }
- if (index < [self numberOfPhotos] - 1) {
-
- for (i = index + 2; i < _photos.count; i++) {
- id photo = [_photos objectAtIndex:i];
- if (photo != [NSNull null]) {
- [photo unloadUnderlyingImage];
- [_photos replaceObjectAtIndex:i withObject:[NSNull null]];
- MWLog(@"Released underlying image at index %lu", (unsigned long)i);
- }
- }
- }
-
-
-
- id <MWPhoto> currentPhoto = [self photoAtIndex:index];
- if ([currentPhoto underlyingImage]) {
-
- [self loadAdjacentPhotosIfNecessary:currentPhoto];
- }
-
-
- if (index != _previousPageIndex) {
- if ([_delegate respondsToSelector:@selector(photoBrowser:didDisplayPhotoAtIndex:)])
- [_delegate photoBrowser:self didDisplayPhotoAtIndex:index];
- _previousPageIndex = index;
- } else {
- if ([_delegate respondsToSelector:@selector(photoBrowser:didDisplayPhotoAtIndex:)])
- [_delegate photoBrowser:self didDisplayPhotoAtIndex:index];
- }
-
-
- [self updateNavigation];
- }
- #pragma mark - Frame Calculations
- - (CGRect)frameForPagingScrollView {
- CGRect frame = self.view.bounds;
- frame.origin.x -= PADDING;
- frame.size.width += (2 * PADDING);
- return CGRectIntegral(frame);
- }
- - (CGRect)frameForPageAtIndex:(NSUInteger)index {
-
-
-
-
- CGRect bounds = _pagingScrollView.bounds;
- CGRect pageFrame = bounds;
- pageFrame.size.width -= (2 * PADDING);
- pageFrame.origin.x = (bounds.size.width * index) + PADDING;
- return CGRectIntegral(pageFrame);
- }
- - (CGSize)contentSizeForPagingScrollView {
-
- CGRect bounds = _pagingScrollView.bounds;
- return CGSizeMake(bounds.size.width * [self numberOfPhotos], bounds.size.height);
- }
- - (CGPoint)contentOffsetForPageAtIndex:(NSUInteger)index {
- CGFloat pageWidth = _pagingScrollView.bounds.size.width;
- CGFloat newOffset = index * pageWidth;
- return CGPointMake(newOffset, 0);
- }
- - (CGRect)frameForToolbarAtOrientation:(UIInterfaceOrientation)orientation {
- CGFloat height = 49;
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone &&
- UIInterfaceOrientationIsLandscape(orientation)) height = 49;
- return CGRectIntegral(CGRectMake(0, self.view.bounds.size.height - height, self.view.bounds.size.width, height));
- }
- - (CGRect)frameForCaptionView:(MWCaptionView *)captionView atIndex:(NSUInteger)index {
- CGRect pageFrame = [self frameForPageAtIndex:index];
- CGSize captionSize = [captionView sizeThatFits:CGSizeMake(pageFrame.size.width, 0)];
- CGRect captionFrame = CGRectMake(pageFrame.origin.x,
- pageFrame.size.height - captionSize.height - (_toolbar.superview?_toolbar.frame.size.height:0),
- pageFrame.size.width,
- captionSize.height);
- return CGRectIntegral(captionFrame);
- }
- - (CGRect)frameForSelectedButton:(UIButton *)selectedButton atIndex:(NSUInteger)index {
- CGRect pageFrame = [self frameForPageAtIndex:index];
- CGFloat padding = 20;
- CGFloat yOffset = 0;
- if (![self areControlsHidden]) {
- UINavigationBar *navBar = self.navigationController.navigationBar;
- yOffset = navBar.frame.origin.y + navBar.frame.size.height;
- }
- CGRect selectedButtonFrame = CGRectMake(pageFrame.origin.x + pageFrame.size.width - selectedButton.frame.size.width - padding,
- padding + yOffset,
- selectedButton.frame.size.width,
- selectedButton.frame.size.height);
- return CGRectIntegral(selectedButtonFrame);
- }
- - (CGRect)frameForPlayButton:(UIButton *)playButton atIndex:(NSUInteger)index {
- CGRect pageFrame = [self frameForPageAtIndex:index];
- return CGRectMake(floorf(CGRectGetMidX(pageFrame) - playButton.frame.size.width / 2),
- floorf(CGRectGetMidY(pageFrame) - playButton.frame.size.height / 2),
- playButton.frame.size.width,
- playButton.frame.size.height);
- }
- #pragma mark - UIScrollView Delegate
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
-
-
- if (!_viewIsActive || _performingLayout || _rotating) return;
-
-
- [self tilePages];
-
-
- CGRect visibleBounds = _pagingScrollView.bounds;
- NSInteger index = (NSInteger)(floorf(CGRectGetMidX(visibleBounds) / CGRectGetWidth(visibleBounds)));
- if (index < 0) index = 0;
- if (index > [self numberOfPhotos] - 1) index = [self numberOfPhotos] - 1;
- NSUInteger previousCurrentPage = _currentPageIndex;
- _currentPageIndex = index;
- if (_currentPageIndex != previousCurrentPage) {
- [self didStartViewingPageAtIndex:index];
- }
-
- }
- - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
-
- [self setControlsHidden:YES animated:YES permanent:NO];
- }
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
-
- [self updateNavigation];
- }
- #pragma mark - Navigation
- - (void)updateNavigation {
-
-
- NSUInteger numberOfPhotos = [self numberOfPhotos];
- if (_gridController) {
- if (_gridController.selectionMode) {
- self.title = NSLocalizedString(@"Select Photos", nil);
- } else {
- NSString *photosText;
- if (numberOfPhotos == 1) {
- photosText = NSLocalizedString(@"photo", @"Used in the context: '1 photo'");
- } else {
- photosText = NSLocalizedString(@"photos", @"Used in the context: '3 photos'");
- }
- self.title = [NSString stringWithFormat:@"%lu %@", (unsigned long)numberOfPhotos, photosText];
- }
- } else if (numberOfPhotos > 1) {
- if ([_delegate respondsToSelector:@selector(photoBrowser:titleForPhotoAtIndex:)]) {
- self.title = [_delegate photoBrowser:self titleForPhotoAtIndex:_currentPageIndex];
- } else {
- self.title = [NSString stringWithFormat:@"%lu %@ %lu", (unsigned long)(_currentPageIndex+1), NSLocalizedString(@"of", @"Used in the context: 'Showing 1 of 3 items'"), (unsigned long)numberOfPhotos];
- }
- } else {
- self.title = nil;
- }
-
-
- _previousButton.enabled = (_currentPageIndex > 0);
- _nextButton.enabled = (_currentPageIndex < numberOfPhotos - 1);
-
-
-
- }
- - (void)jumpToPageAtIndex:(NSUInteger)index animated:(BOOL)animated {
-
-
- if (index < [self numberOfPhotos]) {
- CGRect pageFrame = [self frameForPageAtIndex:index];
- [_pagingScrollView setContentOffset:CGPointMake(pageFrame.origin.x - PADDING, 0) animated:animated];
- [self updateNavigation];
- }
-
-
- [self hideControlsAfterDelay];
-
- }
- - (void)gotoPreviousPage {
- [self showPreviousPhotoAnimated:NO];
- }
- - (void)gotoNextPage {
- [self showNextPhotoAnimated:NO];
- }
- - (void)showPreviousPhotoAnimated:(BOOL)animated {
- [self jumpToPageAtIndex:_currentPageIndex-1 animated:animated];
- }
- - (void)showNextPhotoAnimated:(BOOL)animated {
- [self jumpToPageAtIndex:_currentPageIndex+1 animated:animated];
- }
- #pragma mark - Interactions
- - (void)selectedButtonTapped:(id)sender {
- UIButton *selectedButton = (UIButton *)sender;
- selectedButton.selected = !selectedButton.selected;
- NSUInteger index = NSUIntegerMax;
- for (MWZoomingScrollView *page in _visiblePages) {
- if (page.selectedButton == selectedButton) {
- index = page.index;
- break;
- }
- }
- if (index != NSUIntegerMax) {
- [self setPhotoSelected:selectedButton.selected atIndex:index];
- }
- }
- - (void)playButtonTapped:(id)sender {
- UIButton *playButton = (UIButton *)sender;
- NSUInteger index = NSUIntegerMax;
- for (MWZoomingScrollView *page in _visiblePages) {
- if (page.playButton == playButton) {
- index = page.index;
- break;
- }
- }
- if (index != NSUIntegerMax) {
- if (!_currentVideoPlayerViewController) {
- [self playVideoAtIndex:index];
- }
- }
- }
- #pragma mark - Video
- - (void)playVideoAtIndex:(NSUInteger)index {
- id photo = [self photoAtIndex:index];
- if ([photo respondsToSelector:@selector(getVideoURL:)]) {
-
-
- _currentVideoIndex = index;
- [self clearCurrentVideo];
- [self setVideoLoadingIndicatorVisible:YES atPageIndex:index];
-
-
- [photo getVideoURL:^(NSURL *url) {
- if (url) {
- dispatch_async(dispatch_get_main_queue(), ^{
- [self _playVideo:url atPhotoIndex:index];
- });
- } else {
- [self setVideoLoadingIndicatorVisible:NO atPageIndex:index];
- }
- }];
-
- }
- }
- - (void)_playVideo:(NSURL *)videoURL atPhotoIndex:(NSUInteger)index {
-
- _currentVideoPlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
- [_currentVideoPlayerViewController.moviePlayer prepareToPlay];
- _currentVideoPlayerViewController.moviePlayer.shouldAutoplay = YES;
- _currentVideoPlayerViewController.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
- _currentVideoPlayerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
-
-
-
- [[NSNotificationCenter defaultCenter] removeObserver:_currentVideoPlayerViewController
- name:MPMoviePlayerPlaybackDidFinishNotification
- object:_currentVideoPlayerViewController.moviePlayer];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(videoFinishedCallback:)
- name:MPMoviePlayerPlaybackDidFinishNotification
- object:_currentVideoPlayerViewController.moviePlayer];
-
- [self presentViewController:_currentVideoPlayerViewController animated:YES completion:nil];
- }
- - (void)videoFinishedCallback:(NSNotification*)notification {
-
-
- [[NSNotificationCenter defaultCenter] removeObserver:self
- name:MPMoviePlayerPlaybackDidFinishNotification
- object:_currentVideoPlayerViewController.moviePlayer];
-
-
- [self clearCurrentVideo];
-
-
- [[NSNotificationCenter defaultCenter] postNotificationName:@"closePhotoBrowser" object:nil];
-
-
- BOOL error = [[[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue] == MPMovieFinishReasonPlaybackError;
- if (error) {
-
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [self dismissViewControllerAnimated:YES completion:nil];
- });
- } else {
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- }
- - (void)clearCurrentVideo {
- if (!_currentVideoPlayerViewController) return;
- [_currentVideoLoadingIndicator removeFromSuperview];
- _currentVideoPlayerViewController = nil;
- _currentVideoLoadingIndicator = nil;
- _currentVideoIndex = NSUIntegerMax;
- }
- - (void)setVideoLoadingIndicatorVisible:(BOOL)visible atPageIndex:(NSUInteger)pageIndex {
- if (_currentVideoLoadingIndicator && !visible) {
- [_currentVideoLoadingIndicator removeFromSuperview];
- _currentVideoLoadingIndicator = nil;
- } else if (!_currentVideoLoadingIndicator && visible) {
- _currentVideoLoadingIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectZero];
- [_currentVideoLoadingIndicator sizeToFit];
- [_currentVideoLoadingIndicator startAnimating];
- [_pagingScrollView addSubview:_currentVideoLoadingIndicator];
- [self positionVideoLoadingIndicator];
- }
- }
- - (void)positionVideoLoadingIndicator {
- if (_currentVideoLoadingIndicator && _currentVideoIndex != NSUIntegerMax) {
- CGRect frame = [self frameForPageAtIndex:_currentVideoIndex];
- _currentVideoLoadingIndicator.center = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame));
- }
- }
- #pragma mark - Grid
- - (BOOL)isGridController {
- return (_gridController ? true : false);
- }
- - (void)showGridAnimated {
- [self showGrid:YES];
- }
- - (void)showGrid:(BOOL)animated {
- if (_gridController) return;
-
-
- _gridController = [[MWGridViewController alloc] init];
- _gridController.initialContentOffset = _currentGridContentOffset;
- _gridController.browser = self;
- _gridController.selectionMode = _displaySelectionButtons;
- _gridController.view.frame = self.view.bounds;
- _gridController.view.frame = CGRectOffset(_gridController.view.frame, 0, (self.startOnGrid ? -1 : 1) * self.view.bounds.size.height);
-
- _skipNextPagingScrollViewPositioning = YES;
-
-
- [self addChildViewController:_gridController];
- [self.view addSubview:_gridController.view];
-
-
- [_gridController.view layoutIfNeeded];
- [_gridController adjustOffsetsAsRequired];
-
-
- if (self.navigationItem.rightBarButtonItem == _actionButton) {
- _gridPreviousRightNavItem = _actionButton;
- [self.navigationItem setRightBarButtonItem:nil animated:YES];
- } else {
- _gridPreviousRightNavItem = nil;
- }
-
-
- [self updateNavigation];
- [self setControlsHidden:NO animated:YES permanent:YES];
-
-
- [_gridController willMoveToParentViewController:self];
- [UIView animateWithDuration:animated ? 0.3 : 0 animations:^(void) {
- _gridController.view.frame = self.view.bounds;
- CGRect newPagingFrame = [self frameForPagingScrollView];
- newPagingFrame = CGRectOffset(newPagingFrame, 0, (self.startOnGrid ? 1 : -1) * newPagingFrame.size.height);
- _pagingScrollView.frame = newPagingFrame;
- } completion:^(BOOL finished) {
- [_gridController didMoveToParentViewController:self];
- }];
-
-
- if ([_delegate respondsToSelector:@selector(gridWillAppear:)]) {
-
- [_delegate gridWillAppear:self];
- }
- }
- - (void)hideGrid {
-
- if (!_gridController) return;
-
-
- _currentGridContentOffset = _gridController.collectionView.contentOffset;
-
-
- if (_gridPreviousRightNavItem == _actionButton && _actionButton) {
- [self.navigationItem setRightBarButtonItem:_gridPreviousRightNavItem animated:YES];
- }
-
-
- CGRect newPagingFrame = [self frameForPagingScrollView];
- newPagingFrame = CGRectOffset(newPagingFrame, 0, (self.startOnGrid ? 1 : -1) * newPagingFrame.size.height);
- _pagingScrollView.frame = newPagingFrame;
-
-
- MWGridViewController *tmpGridController = _gridController;
- _gridController = nil;
-
-
- [self updateNavigation];
- [self updateVisiblePageStates];
-
-
- [UIView animateWithDuration:0.3 animations:^{
- tmpGridController.view.frame = CGRectOffset(self.view.bounds, 0, (self.startOnGrid ? -1 : 1) * self.view.bounds.size.height);
- _pagingScrollView.frame = [self frameForPagingScrollView];
- } completion:^(BOOL finished) {
- [tmpGridController willMoveToParentViewController:nil];
- [tmpGridController.view removeFromSuperview];
- [tmpGridController removeFromParentViewController];
- [self setControlsHidden:NO animated:YES permanent:NO];
- }];
-
- if ([_delegate respondsToSelector:@selector(gridWillDisappear:)]) {
-
- [_delegate gridWillDisappear:self];
- }
- }
- #pragma mark - Control Hiding / Showing
- - (void)setControlsHidden:(BOOL)hidden animated:(BOOL)animated permanent:(BOOL)permanent {
-
-
- if (![self numberOfPhotos] || _gridController || _alwaysShowControls)
- hidden = NO;
-
-
- [self cancelControlHiding];
-
-
- CGFloat animatonOffset = 20;
- CGFloat animationDuration = (animated ? 0.35 : 0);
-
-
- if (!_leaveStatusBarAlone) {
-
- if (!_isVCBasedStatusBarAppearance) {
-
-
-
-
- } else {
-
-
-
- _statusBarShouldBeHidden = hidden;
-
-
-
-
-
- }
- }
-
-
-
- if ([self areControlsHidden] && !hidden && animated) {
-
-
- _toolbar.frame = CGRectOffset([self frameForToolbarAtOrientation:[[UIApplication sharedApplication] statusBarOrientation]], 0, animatonOffset);
-
-
- for (MWZoomingScrollView *page in _visiblePages) {
-
- if (page.captionView) {
-
- MWCaptionView *v = page.captionView;
-
-
- id <MWPhoto> photo = [self photoAtIndex:self.currentIndex];
- if (photo.caption) {
-
- if ([photo caption]) v.label.text = photo.caption;
- }
-
-
- CGRect captionFrame = [self frameForCaptionView:v atIndex:0];
- captionFrame.origin.x = v.frame.origin.x;
- v.frame = CGRectOffset(captionFrame, 0, animatonOffset);
- }
- }
-
- }
-
- if ([_delegate respondsToSelector:@selector(setControlsHidden:animated:permanent:)]) {
- [_delegate setControlsHidden:hidden animated:animated permanent:permanent];
- }
-
- [UIView animateWithDuration:animationDuration animations:^(void) {
-
- CGFloat alpha = hidden ? 0 : 1;
-
- [self.navigationController.navigationBar setAlpha:alpha];
-
-
- _toolbar.frame = [self frameForToolbarAtOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
- if (hidden) _toolbar.frame = CGRectOffset(_toolbar.frame, 0, animatonOffset);
- _toolbar.alpha = alpha;
-
- for (MWZoomingScrollView *page in _visiblePages) {
- if (page.captionView) {
- MWCaptionView *v = page.captionView;
-
- CGRect captionFrame = [self frameForCaptionView:v atIndex:0];
- captionFrame.origin.x = v.frame.origin.x;
- if (hidden) captionFrame = CGRectOffset(captionFrame, 0, animatonOffset);
- v.frame = captionFrame;
- v.alpha = alpha;
- }
- }
-
-
- for (MWZoomingScrollView *page in _visiblePages) {
- if (page.selectedButton) {
- UIButton *v = page.selectedButton;
- CGRect newFrame = [self frameForSelectedButton:v atIndex:0];
- newFrame.origin.x = v.frame.origin.x;
- v.frame = newFrame;
- }
- }
- } completion:^(BOOL finished) {}];
-
-
-
-
- if (!permanent) [self hideControlsAfterDelay];
-
- }
- - (BOOL)prefersStatusBarHidden {
- if (!_leaveStatusBarAlone) {
- return _statusBarShouldBeHidden;
- } else {
- return [self presentingViewControllerPrefersStatusBarHidden];
- }
- }
- - (UIStatusBarStyle)preferredStatusBarStyle {
- return UIStatusBarStyleLightContent;
- }
- - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
- return UIStatusBarAnimationSlide;
- }
- - (void)cancelControlHiding {
-
- if (_controlVisibilityTimer) {
- [_controlVisibilityTimer invalidate];
- _controlVisibilityTimer = nil;
- }
- }
- - (void)hideControlsAfterDelay {
- if (![self areControlsHidden]) {
- [self cancelControlHiding];
- _controlVisibilityTimer = [NSTimer scheduledTimerWithTimeInterval:self.delayToHideElements target:self selector:@selector(hideControls) userInfo:nil repeats:NO];
- }
- }
- - (BOOL)areControlsHidden {
- return (_toolbar.alpha == 0);
- }
- - (void)hideControls { [self setControlsHidden:YES animated:YES permanent:NO]; }
- - (void)showControls { [self setControlsHidden:NO animated:YES permanent:NO]; }
- - (void)toggleControls { [self setControlsHidden:![self areControlsHidden] animated:YES permanent:NO]; }
- #pragma mark - Properties
- - (void)setCurrentPhotoIndex:(NSUInteger)index {
-
- NSUInteger photoCount = [self numberOfPhotos];
- if (photoCount == 0) {
- index = 0;
- } else {
- if (index >= photoCount)
- index = [self numberOfPhotos]-1;
- }
- _currentPageIndex = index;
- if ([self isViewLoaded]) {
- [self jumpToPageAtIndex:index animated:NO];
- if (!_viewIsActive)
- [self tilePages];
- }
- }
- #pragma mark - Misc
- - (void)doneButtonPressed:(id)sender {
-
- if (_doneButton) {
-
- if (self.enableGrid) {
- if (self.startOnGrid && !_gridController) {
- [self showGrid:YES];
- return;
- } else if (!self.startOnGrid && _gridController) {
- [self hideGrid];
- return;
- }
- }
-
- if ([_delegate respondsToSelector:@selector(photoBrowserDidFinishModalPresentation:)]) {
-
- [_delegate photoBrowserDidFinishModalPresentation:self];
- } else {
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- }
- }
- #pragma mark - Delete
- - (void)deleteButtonPressed:(id)sender {
-
- if ([self.delegate respondsToSelector:@selector(photoBrowser:deleteButtonPressedForPhotoAtIndex:deleteButton:)])
- [self.delegate photoBrowser:self deleteButtonPressedForPhotoAtIndex:_currentPageIndex deleteButton:self.deleteButton];
- }
- #pragma mark - Actions
- - (void)actionButtonPressed:(id)sender {
-
- id <MWPhoto> photo = [self photoAtIndex:_currentPageIndex];
- if ([self numberOfPhotos] > 0 && [photo underlyingImage]) {
-
-
- if ([self.delegate respondsToSelector:@selector(photoBrowser:actionButtonPressedForPhotoAtIndex:)]) {
-
-
- [self.delegate photoBrowser:self actionButtonPressedForPhotoAtIndex:_currentPageIndex];
-
- } else {
-
-
- NSMutableArray *items = [NSMutableArray arrayWithObject:[photo underlyingImage]];
- if (photo.caption) {
- [items addObject:photo.caption];
- }
- self.activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
-
-
- typeof(self) __weak weakSelf = self;
- [self.activityViewController setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
- weakSelf.activityViewController = nil;
- [weakSelf hideControlsAfterDelay];
- }];
-
- if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8")) {
- self.activityViewController.popoverPresentationController.barButtonItem = _actionButton;
- }
- [self presentViewController:self.activityViewController animated:YES completion:nil];
- }
-
-
- [self setControlsHidden:NO animated:YES permanent:YES];
- }
-
- }
- @end
|