Skip to content

Commit

Permalink
Fixed several bugs. Added demo menu screen. Added arrow images for to…
Browse files Browse the repository at this point in the history
…olbar buttons.
  • Loading branch information
mwaterfall committed Oct 21, 2010
1 parent 52daf15 commit c822fa0
Show file tree
Hide file tree
Showing 15 changed files with 515 additions and 264 deletions.
8 changes: 4 additions & 4 deletions Classes/MWPhotoBrowser.h
Expand Up @@ -25,11 +25,8 @@
int pageIndexBeforeRotation;

// Navigation & controls
UIToolbar *toolbar;
NSTimer *controlVisibilityTimer;

// Misc
UIStatusBarStyle previousStatusBarStyle;
BOOL previousToolbarVisibility;

}

Expand All @@ -39,6 +36,9 @@
// Photos
- (UIImage *)imageAtIndex:(int)index;

// Layout
- (void)performLayout;

// Paging
- (void)tilePages;
- (BOOL)isDisplayingPageForIndex:(int)index;
Expand Down
126 changes: 80 additions & 46 deletions Classes/MWPhotoBrowser.m
Expand Up @@ -9,12 +9,14 @@
#import "MWPhotoBrowser.h"
#import "ZoomingScrollView.h"

#define PADDING 10
#define PADDING 10

// Handle depreciations and supress hide warnings
@interface UIApplication (DepreciationWarningSuppresion)
- (void)setStatusBarHidden:(BOOL)hidden animated:(BOOL)animated;
@end

// MWPhotoBrowser
@implementation MWPhotoBrowser

- (id)initWithPhotos:(NSArray *)photosArray {
Expand All @@ -26,7 +28,6 @@ - (id)initWithPhotos:(NSArray *)photosArray {
// Defaults
self.wantsFullScreenLayout = YES;
currentPageIndex = 0;
previousStatusBarStyle = UIStatusBarStyleDefault;

}
return self;
Expand Down Expand Up @@ -55,13 +56,15 @@ - (void)viewDidUnload {
[pagingScrollView release];
[visiblePages release];
[recycledPages release];
[toolbar release];
}

- (void)dealloc {
[photos release];
[pagingScrollView release];
[visiblePages release];
[recycledPages release];
[toolbar release];
[super dealloc];
}

Expand Down Expand Up @@ -91,21 +94,27 @@ - (void)viewDidLoad {
recycledPages = [[NSMutableSet alloc] init];
[self tilePages];

// Bars
// Navigation Bar
self.navigationController.navigationBar.tintColor = nil;
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;

// Toolbar
toolbar = [[UIToolbar alloc] initWithFrame:[self frameForToolbarAtOrientation:self.interfaceOrientation]];
toolbar.tintColor = nil;
toolbar.barStyle = UIBarStyleBlackTranslucent;
[self.view addSubview:toolbar];

// Toolbar Items
UIBarButtonItem *previousImage = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(gotoPreviousPage)];
UIBarButtonItem *nextImage = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(gotoNextPage)];
UIBarButtonItem *previousImage = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"UIBarButtonItemArrowLeft.png"] style:UIBarButtonItemStylePlain target:self action:@selector(gotoPreviousPage)];
UIBarButtonItem *nextImage = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"UIBarButtonItemArrowRight.png"] style:UIBarButtonItemStylePlain target:self action:@selector(gotoNextPage)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:space];
if (photos.count > 1) [items addObject:previousImage];
[items addObject:space];
if (photos.count > 1) [items addObject:nextImage];
[items addObject:space];
[self setToolbarItems:items];
[toolbar setItems:items];
[items release];
[previousImage release], [nextImage release], [space release];

Expand All @@ -114,19 +123,16 @@ - (void)viewDidLoad {

}


- (void)viewWillAppear:(BOOL)animated {

// Super
[super viewWillAppear:animated];

// Set status bar style to black translucent and remember previous
previousStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
// Layout
[self performLayout];

// Show Toolbar
previousToolbarVisibility = self.navigationController.toolbarHidden;
self.navigationController.toolbarHidden = NO;
// Set status bar style to black translucent
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];

// Navigation
[self updateNavigation];
Expand All @@ -143,12 +149,44 @@ - (void)viewWillDisappear:(BOOL)animated {
// Cancel any hiding timers
[self cancelControlHiding];

// Reset Toolbar
self.navigationController.toolbarHidden = previousToolbarVisibility;
}

#pragma mark -
#pragma mark Layout

// Layout subviews
- (void)performLayout {

// Toolbar
toolbar.frame = [self frameForToolbarAtOrientation:self.interfaceOrientation];

// Remember index
int indexPriorToLayout = currentPageIndex;

// Get paging scroll view frame to determine if anything needs changing
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];

// Frame needs changing
pagingScrollView.frame = pagingScrollViewFrame;

// Reset status bar style
[[UIApplication sharedApplication] setStatusBarStyle:previousStatusBarStyle animated:YES];
// Recalculate contentSize based on current orientation
pagingScrollView.contentSize = [self contentSizeForPagingScrollView];

// Adjust frames and configuration of each visible page
for (ZoomingScrollView *page in visiblePages) {
page.frame = [self frameForPageAtIndex:page.index];
[page setMaxMinZoomScalesForCurrentBounds];
page.zoomScale = page.minimumZoomScale;
}

// adjust contentOffset to preserve page location based on values collected prior to location
CGFloat pageWidth = pagingScrollView.bounds.size.width;
CGFloat newOffset = indexPriorToLayout * pageWidth;
pagingScrollView.contentOffset = CGPointMake(newOffset, 0);

// Reset page
currentPageIndex = indexPriorToLayout;

}

#pragma mark -
Expand Down Expand Up @@ -180,19 +218,23 @@ - (void)photoDidFinishLoading:(MWPhoto *)photo {

// Tell page to display image again
ZoomingScrollView *page = [self pageDisplayedAtIndex:index];
if (page) {

// Display
[page displayImage];

}
if (page) [page displayImage];

}
}
}

- (void)photoDidFailToLoad:(MWPhoto *)photo {
NSLog(@"MWPhotoBrowser: Photo failed to load in background");
int index = [photos indexOfObject:photo];
if (index != NSNotFound) {
if ([self isDisplayingPageForIndex:index]) {

// Tell page it failed
ZoomingScrollView *page = [self pageDisplayedAtIndex:index];
if (page) [page displayImageFailure];

}
}
}

#pragma mark -
Expand All @@ -213,7 +255,7 @@ - (void)tilePages {
for (ZoomingScrollView *page in visiblePages) {
if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
[recycledPages addObject:page];
NSLog(@"Removed page at index %i", page.index);
/*NSLog(@"Removed page at index %i", page.index);*/
page.index = NSNotFound; // empty
[page removeFromSuperview];
}
Expand All @@ -231,7 +273,7 @@ - (void)tilePages {
[self configurePage:page forIndex:index];
[visiblePages addObject:page];
[pagingScrollView addSubview:page];
NSLog(@"Added page at index %i", page.index);
/*NSLog(@"Added page at index %i", page.index);*/
}
}

Expand Down Expand Up @@ -285,7 +327,7 @@ - (void)didStartViewingPageAtIndex:(int)index {
#pragma mark Frame Calculations

- (CGRect)frameForPagingScrollView {
CGRect frame = [[UIScreen mainScreen] bounds];
CGRect frame = self.view.bounds;// [[UIScreen mainScreen] bounds];
frame.origin.x -= PADDING;
frame.size.width += (2 * PADDING);
return frame;
Expand Down Expand Up @@ -354,8 +396,11 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
- (void)updateNavigation {

// Title
self.title = [NSString stringWithFormat:@"%i of %i", currentPageIndex+1, photos.count];
//navigationBar.topItem.title =
if (photos.count > 1) {
self.title = [NSString stringWithFormat:@"%i of %i", currentPageIndex+1, photos.count];
} else {
self.title = nil;
}

}

Expand Down Expand Up @@ -410,7 +455,7 @@ - (void)setControlsHidden:(BOOL)hidden {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.35];
[self.navigationController.navigationBar setAlpha:hidden ? 0 : 1];
[self.navigationController.toolbar setAlpha:hidden ? 0 : 1];
[toolbar setAlpha:hidden ? 0 : 1];
[UIView commitAnimations];

// Control hiding timer
Expand Down Expand Up @@ -455,21 +500,10 @@ - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrie
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

// recalculate contentSize based on current orientation
pagingScrollView.contentSize = [self contentSizeForPagingScrollView];

// adjust frames and configuration of each visible page
for (ZoomingScrollView *page in visiblePages) {
page.frame = [self frameForPageAtIndex:page.index];
[page setMaxMinZoomScalesForCurrentBounds];
page.zoomScale = page.minimumZoomScale;
}

// adjust contentOffset to preserve page location based on values collected prior to location
CGFloat pageWidth = pagingScrollView.bounds.size.width;
CGFloat newOffset = pageIndexBeforeRotation * pageWidth;
pagingScrollView.contentOffset = CGPointMake(newOffset, 0);

// Perform layout
currentPageIndex = pageIndexBeforeRotation;
[self performLayout];

// Delay control holding
[self hideControlsAfterDelay];
Expand Down
37 changes: 4 additions & 33 deletions Classes/MWPhotoBrowserAppDelegate.m
Expand Up @@ -7,7 +7,7 @@
//

#import "MWPhotoBrowserAppDelegate.h"
#import "MWPhotoBrowser.h"
#import "Menu.h"

@implementation MWPhotoBrowserAppDelegate

Expand All @@ -19,41 +19,12 @@ @implementation MWPhotoBrowserAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

NSArray *photos = [NSArray arrayWithObjects:
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo1l" ofType:@"jpg"]],
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo2l" ofType:@"jpg"]],
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo3l" ofType:@"jpg"]],
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo4l" ofType:@"jpg"]],
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo1l" ofType:@"jpg"]],
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo2l" ofType:@"jpg"]],
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo3l" ofType:@"jpg"]],
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo4l" ofType:@"jpg"]],
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo1l" ofType:@"jpg"]],
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo2l" ofType:@"jpg"]],
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo3l" ofType:@"jpg"]],
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo4l" ofType:@"jpg"]],
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo1l" ofType:@"jpg"]],
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo2l" ofType:@"jpg"]],
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo3l" ofType:@"jpg"]],
[MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"photo4l" ofType:@"jpg"]],
nil];

// NSArray *photos = [NSArray arrayWithObjects:
// [MWPhoto photoWithURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3567/3523321514_371d9ac42f_b.jpg"]],
// [MWPhoto photoWithURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3629/3339128908_7aecabc34b_b.jpg"]],
// [MWPhoto photoWithURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3364/3338617424_7ff836d55f_b.jpg"]],
// [MWPhoto photoWithURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3590/3329114220_5fbc5bc92b_b.jpg"]],
// nil];

MWPhotoBrowser *vc = [[MWPhotoBrowser alloc] initWithPhotos:photos];
Menu *vc = [[Menu alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];

// Add the view controller's view to the window and display.
[window addSubview:nc.view];
[window addSubview:nc.view];
[window makeKeyAndVisible];

return YES;

}


Expand Down
15 changes: 15 additions & 0 deletions Classes/Menu.h
@@ -0,0 +1,15 @@
//
// Menu.h
// MWPhotoBrowser
//
// Created by Michael Waterfall on 21/10/2010.
// Copyright 2010 d3i. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface Menu : UITableViewController {

}

@end

0 comments on commit c822fa0

Please sign in to comment.