-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNIToolbarPhotoViewController.m
573 lines (431 loc) · 21.1 KB
/
NIToolbarPhotoViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//
// Copyright 2011 Jeff Verkoeyen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "NIToolbarPhotoViewController.h"
#import "NIPhotoAlbumScrollView.h"
#import "NimbusCore.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation NIToolbarPhotoViewController
@synthesize showPhotoAlbumBeneathToolbar = _showPhotoAlbumBeneathToolbar;
@synthesize hidesChromeWhenScrolling = _hidesChromeWhenScrolling;
@synthesize chromeCanBeHidden = _chromeCanBeHidden;
@synthesize animateMovingToNextAndPreviousPhotos = _animateMovingToNextAndPreviousPhotos;
@synthesize scrubberIsEnabled = _scrubberIsEnabled;
@synthesize toolbar = _toolbar;
@synthesize photoAlbumView = _photoAlbumView;
@synthesize photoScrubberView = _photoScrubberView;
@synthesize nextButton = _nextButton;
@synthesize previousButton = _previousButton;
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)shutdown {
_toolbar = nil;
_photoAlbumView = nil;
NI_RELEASE_SAFELY(_nextButton);
NI_RELEASE_SAFELY(_previousButton);
NI_RELEASE_SAFELY(_photoScrubberView);
NI_RELEASE_SAFELY(_tapGesture);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc {
[self shutdown];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Default Configuration Settings
self.showPhotoAlbumBeneathToolbar = YES;
self.hidesChromeWhenScrolling = YES;
self.chromeCanBeHidden = YES;
self.animateMovingToNextAndPreviousPhotos = NO;
// The scrubber is better use of the extra real estate on the iPad.
// If you ask me, though, the scrubber works pretty well on the iPhone too. It's up
// to you if you want to use it in your own implementations.
self.scrubberIsEnabled = NIIsPad();
// Allow the photos to display beneath the status bar.
self.wantsFullScreenLayout = YES;
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addTapGestureToView {
if ([self isViewLoaded]
&& nil != NIUITapGestureRecognizerClass()
&& [self.photoAlbumView respondsToSelector:@selector(addGestureRecognizer:)]) {
if (nil == _tapGesture) {
_tapGesture =
[[NIUITapGestureRecognizerClass() alloc] initWithTarget: self
action: @selector(didTap)];
[self.photoAlbumView addGestureRecognizer:_tapGesture];
}
}
[_tapGesture setEnabled:YES];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)updateToolbarItems {
UIBarItem* flexibleSpace =
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace
target: nil
action: nil] autorelease];
if ([self isScrubberEnabled]) {
NI_RELEASE_SAFELY(_nextButton);
NI_RELEASE_SAFELY(_previousButton);
if (nil == _photoScrubberView) {
CGRect scrubberFrame = CGRectMake(0, 0,
self.toolbar.bounds.size.width,
self.toolbar.bounds.size.height);
_photoScrubberView = [[NIPhotoScrubberView alloc] initWithFrame:scrubberFrame];
_photoScrubberView.autoresizingMask = (UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleHeight);
_photoScrubberView.delegate = self;
}
UIBarButtonItem* scrubberItem =
[[[UIBarButtonItem alloc] initWithCustomView:self.photoScrubberView] autorelease];
self.toolbar.items = [NSArray arrayWithObjects:
flexibleSpace, scrubberItem, flexibleSpace,
nil];
[_photoScrubberView setSelectedPhotoIndex:self.photoAlbumView.centerPhotoIndex];
} else {
NI_RELEASE_SAFELY(_photoScrubberView);
if (nil == _nextButton) {
UIImage* nextIcon = [UIImage imageWithContentsOfFile:
NIPathForBundleResource(nil, @"NimbusPhotos.bundle/gfx/next.png")];
// We weren't able to find the next or previous icons in your application's resources.
// Ensure that you've dragged the NimbusPhotos.bundle from src/photos/resources into your
// application with the "Create Folder References" option selected. You can verify that
// you've done this correctly by expanding the NimbusPhotos.bundle file in your project
// and verifying that the 'gfx' directory is blue. Also verify that the bundle is being
// copied in the Copy Bundle Resources phase.
NIDASSERT(nil != nextIcon);
_nextButton = [[UIBarButtonItem alloc] initWithImage: nextIcon
style: UIBarButtonItemStylePlain
target: self
action: @selector(didTapNextButton)];
}
if (nil == _previousButton) {
UIImage* previousIcon = [UIImage imageWithContentsOfFile:
NIPathForBundleResource(nil, @"NimbusPhotos.bundle/gfx/previous.png")];
// We weren't able to find the next or previous icons in your application's resources.
// Ensure that you've dragged the NimbusPhotos.bundle from src/photos/resources into your
// application with the "Create Folder References" option selected. You can verify that
// you've done this correctly by expanding the NimbusPhotos.bundle file in your project
// and verifying that the 'gfx' directory is blue. Also verify that the bundle is being
// copied in the Copy Bundle Resources phase.
NIDASSERT(nil != previousIcon);
_previousButton = [[UIBarButtonItem alloc] initWithImage: previousIcon
style: UIBarButtonItemStylePlain
target: self
action: @selector(didTapPreviousButton)];
}
self.toolbar.items = [NSArray arrayWithObjects:
flexibleSpace, self.previousButton,
flexibleSpace, self.nextButton,
flexibleSpace,
nil];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)loadView {
[super loadView];
CGRect bounds = self.view.bounds;
// Toolbar Setup
CGFloat toolbarHeight = NIToolbarHeightForOrientation(NIInterfaceOrientation());
CGRect toolbarFrame = CGRectMake(0, bounds.size.height - toolbarHeight,
bounds.size.width, toolbarHeight);
_toolbar = [[[UIToolbar alloc] initWithFrame:toolbarFrame] autorelease];
_toolbar.barStyle = UIBarStyleBlack;
_toolbar.translucent = self.showPhotoAlbumBeneathToolbar;
_toolbar.autoresizingMask = (UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleTopMargin);
[self updateToolbarItems];
// Photo Album View Setup
CGRect photoAlbumFrame = bounds;
if (!self.showPhotoAlbumBeneathToolbar) {
photoAlbumFrame = NIRectContract(bounds, 0, toolbarHeight);
}
_photoAlbumView = [[[NIPhotoAlbumScrollView alloc] initWithFrame:photoAlbumFrame] autorelease];
_photoAlbumView.autoresizingMask = (UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleHeight);
_photoAlbumView.delegate = self;
[self.view addSubview:_photoAlbumView];
[self.view addSubview:_toolbar];
if (self.hidesChromeWhenScrolling) {
[self addTapGestureToView];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidUnload {
[self shutdown];
[super viewDidUnload];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarStyle: (NIIsPad()
? UIStatusBarStyleBlackOpaque
: UIStatusBarStyleBlackTranslucent)
animated: animated];
UINavigationBar* navBar = self.navigationController.navigationBar;
navBar.barStyle = UIBarStyleBlack;
navBar.translucent = YES;
_previousButton.enabled = [self.photoAlbumView hasPrevious];
_nextButton.enabled = [self.photoAlbumView hasNext];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return NIIsSupportedOrientation(toInterfaceOrientation);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation
duration: (NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self.photoAlbumView willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation
duration: (NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation
duration:duration];
CGRect toolbarFrame = self.toolbar.frame;
toolbarFrame.size.height = NIToolbarHeightForOrientation(toInterfaceOrientation);
toolbarFrame.origin.y = self.view.bounds.size.height - toolbarFrame.size.height;
self.toolbar.frame = toolbarFrame;
if (!self.showPhotoAlbumBeneathToolbar) {
CGRect photoAlbumFrame = self.photoAlbumView.frame;
photoAlbumFrame.size.height = self.view.bounds.size.height - toolbarFrame.size.height;
self.photoAlbumView.frame = photoAlbumFrame;
}
[self.photoAlbumView willAnimateRotationToInterfaceOrientation: toInterfaceOrientation
duration: duration];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didHideChrome {
_isAnimatingChrome = NO;
self.toolbar.hidden = YES;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didShowChrome {
_isAnimatingChrome = NO;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setChromeVisibility:(BOOL)isVisible animated:(BOOL)animated {
if (_isAnimatingChrome
|| (!isVisible && self.toolbar.hidden)
|| (isVisible && !self.toolbar.hidden)
|| !self.chromeCanBeHidden) {
// Nothing to do here.
return;
}
CGRect toolbarFrame = self.toolbar.frame;
CGRect bounds = self.view.bounds;
// Reset the toolbar's initial position.
if (!isVisible) {
toolbarFrame.origin.y = bounds.size.height - toolbarFrame.size.height;
} else {
// Ensure that the toolbar is visible through the animation.
self.toolbar.hidden = NO;
toolbarFrame.origin.y = bounds.size.height;
}
self.toolbar.frame = toolbarFrame;
// Show/hide the system chrome.
if ([[UIApplication sharedApplication] respondsToSelector:
@selector(setStatusBarHidden:withAnimation:)]) {
// On 3.2 and higher we can slide the status bar out.
[[UIApplication sharedApplication] setStatusBarHidden: !isVisible
withAnimation: (animated
? UIStatusBarAnimationSlide
: UIStatusBarAnimationNone)];
} else {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < NIIOS_3_2
// On 3.0 devices we use the boring fade animation.
[[UIApplication sharedApplication] setStatusBarHidden: !isVisible
animated: animated];
#endif
}
// Place the toolbar at its final location.
if (isVisible) {
// Slide up.
toolbarFrame.origin.y = bounds.size.height - toolbarFrame.size.height;
} else {
// Slide down.
toolbarFrame.origin.y = bounds.size.height;
}
// If there is a navigation bar, place it at its final location.
CGRect navigationBarFrame = CGRectZero;
if (nil != self.navigationController.navigationBar) {
navigationBarFrame = self.navigationController.navigationBar.frame;
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGFloat statusBarHeight = MIN(statusBarFrame.size.width, statusBarFrame.size.height);
if (isVisible) {
navigationBarFrame.origin.y = statusBarHeight;
} else {
navigationBarFrame.origin.y = 0;
}
}
if (animated) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:(isVisible
? @selector(didShowChrome)
: @selector(didHideChrome))];
// Ensure that the animation matches the status bar's.
[UIView setAnimationDuration:NIStatusBarAnimationDuration()];
[UIView setAnimationCurve:NIStatusBarAnimationCurve()];
}
self.toolbar.frame = toolbarFrame;
if (nil != self.navigationController.navigationBar) {
self.navigationController.navigationBar.frame = navigationBarFrame;
self.navigationController.navigationBar.alpha = (isVisible ? 1 : 0);
}
if (animated) {
_isAnimatingChrome = YES;
[UIView commitAnimations];
} else if (!isVisible) {
[self didHideChrome];
} else if (isVisible) {
[self didShowChrome];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)toggleChromeVisibility {
[self setChromeVisibility:(self.toolbar.hidden || _isAnimatingChrome) animated:YES];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark UIGestureRecognizer
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didTap {
SEL selector = @selector(toggleChromeVisibility);
if (self.photoAlbumView.zoomingIsEnabled) {
// Cancel any previous delayed performs so that we don't stack them.
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:selector object:nil];
// We need to delay taking action on the first tap in case a second tap comes in, causing
// a double-tap gesture to be recognized and the photo to be zoomed.
[self performSelector: selector
withObject: nil
afterDelay: 0.3];
} else {
// When zooming is disabled, double-tap-to-zoom is also disabled so we don't have to
// be as careful; just toggle the chrome immediately.
[self toggleChromeVisibility];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)refreshChromeState {
self.previousButton.enabled = [self.photoAlbumView hasPrevious];
self.nextButton.enabled = [self.photoAlbumView hasNext];
self.title = [NSString stringWithFormat:@"%d of %d",
(self.photoAlbumView.centerPhotoIndex + 1),
self.photoAlbumView.numberOfPhotos];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark NIPhotoAlbumScrollViewDelegate
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)photoAlbumScrollViewDidScroll:(NIPhotoAlbumScrollView *)photoAlbumScrollView {
if (self.hidesChromeWhenScrolling) {
[self setChromeVisibility:NO animated:YES];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)photoAlbumScrollView: (NIPhotoAlbumScrollView *)photoAlbumScrollView
didZoomIn: (BOOL)didZoomIn {
// This delegate method is called after a double-tap gesture, so cancel any pending
// single-tap gestures.
[NSObject cancelPreviousPerformRequestsWithTarget: self
selector: @selector(toggleChromeVisibility)
object: nil];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)photoAlbumScrollViewDidChangePages:(NIPhotoAlbumScrollView *)photoAlbumScrollView {
// We animate the scrubber when the chrome won't disappear as a nice touch.
// We don't bother animating if the chrome disappears when scrolling because the user
// will barely see the animation happen.
[self.photoScrubberView setSelectedPhotoIndex: [photoAlbumScrollView centerPhotoIndex]
animated: !self.hidesChromeWhenScrolling];
[self refreshChromeState];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark NIPhotoScrubberViewDelegate
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)photoScrubberViewDidChangeSelection:(NIPhotoScrubberView *)photoScrubberView {
[self.photoAlbumView setCenterPhotoIndex:photoScrubberView.selectedPhotoIndex animated:NO];
[self refreshChromeState];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Actions
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didTapNextButton {
[self.photoAlbumView moveToNextAnimated:self.animateMovingToNextAndPreviousPhotos];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didTapPreviousButton {
[self.photoAlbumView moveToPreviousAnimated:self.animateMovingToNextAndPreviousPhotos];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Public Methods
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setShowPhotoAlbumBeneathToolbar:(BOOL)enabled {
_showPhotoAlbumBeneathToolbar = enabled;
self.toolbar.translucent = enabled;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setHidesChromeWhenScrolling:(BOOL)hidesToolbar {
_hidesChromeWhenScrolling = hidesToolbar;
if (hidesToolbar) {
[self addTapGestureToView];
} else {
[_tapGesture setEnabled:NO];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setChromeCanBeHidden:(BOOL)canBeHidden {
if (nil == NIUITapGestureRecognizerClass()) {
// Don't allow the chrome to be hidden if we can't tap to make it visible again.
canBeHidden = NO;
}
_chromeCanBeHidden = canBeHidden;
if (!canBeHidden) {
self.hidesChromeWhenScrolling = NO;
if ([self isViewLoaded]) {
// Ensure that the toolbar is visible.
self.toolbar.hidden = NO;
CGRect toolbarFrame = self.toolbar.frame;
CGRect bounds = self.view.bounds;
toolbarFrame.origin.y = bounds.size.height - toolbarFrame.size.height;
self.toolbar.frame = toolbarFrame;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setScrubberIsEnabled:(BOOL)enabled {
if (_scrubberIsEnabled != enabled) {
_scrubberIsEnabled = enabled;
if ([self isViewLoaded]) {
[self updateToolbarItems];
}
}
}
@end