-
Notifications
You must be signed in to change notification settings - Fork 0
/
NIPhotoScrubberView.m
506 lines (371 loc) · 17.6 KB
/
NIPhotoScrubberView.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
//
// 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 "NIPhotoScrubberView.h"
#import "NimbusCore.h"
#import <QuartzCore/QuartzCore.h>
static const NSInteger NIPhotoScrubberViewUnknownTag = -1;
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@interface NIPhotoScrubberView()
/**
* @internal
*
* A lightweight method for updating all of the visible thumbnails in the scrubber.
*
* This method will force the scrubber to lay itself out, calculate how many thumbnails might
* be visible, and then lay out the thumbnails and fetch any thumbnail images it can find.
*
* This method should never take much time to run, so it can safely be used in layoutSubviews.
*/
- (void)updateVisiblePhotos;
/**
* @internal
*
* Returns a new, autoreleased image view in the style of this photo scrubber.
*
* This implementation returns an image with a 1px solid white border and a black background.
*/
- (UIImageView *)photoView;
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation NIPhotoScrubberView
@synthesize dataSource = _dataSource;
@synthesize delegate = _delegate;
@synthesize selectedPhotoIndex = _selectedPhotoIndex;
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc {
NI_RELEASE_SAFELY(_visiblePhotoViews);
NI_RELEASE_SAFELY(_recycledPhotoViews);
NI_RELEASE_SAFELY(_containerView);
NI_RELEASE_SAFELY(_selectionView);
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Only one finger should be allowed to interact with the scrubber at a time.
self.multipleTouchEnabled = NO;
_containerView = [[UIView alloc] init];
_containerView.layer.borderColor = [UIColor colorWithWhite:1 alpha:0.1f].CGColor;
_containerView.layer.borderWidth = 1;
_containerView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.3f];
_containerView.userInteractionEnabled = NO;
[self addSubview:_containerView];
_selectionView = [[self photoView] retain];
[self addSubview:_selectionView];
_selectedPhotoIndex = -1;
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark View Creation
///////////////////////////////////////////////////////////////////////////////////////////////////
- (UIImageView *)photoView {
UIImageView* imageView = [[[UIImageView alloc] init] autorelease];
imageView.layer.borderColor = [UIColor whiteColor].CGColor;
imageView.layer.borderWidth = 1;
imageView.backgroundColor = [UIColor blackColor];
imageView.clipsToBounds = YES;
imageView.userInteractionEnabled = NO;
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.tag = NIPhotoScrubberViewUnknownTag;
return imageView;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Layout
///////////////////////////////////////////////////////////////////////////////////////////////////
- (CGSize)photoSize {
CGSize boundsSize = self.bounds.size;
// These numbers are roughly estimated from the Photos.app's scrubber.
CGFloat photoWidth = floorf(boundsSize.height / 2.4f);
CGFloat photoHeight = floorf(photoWidth * 0.75f);
return CGSizeMake(photoWidth, photoHeight);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (CGSize)selectionSize {
CGSize boundsSize = self.bounds.size;
// These numbers are roughly estimated from the Photos.app's scrubber.
CGFloat selectionWidth = floorf(boundsSize.height / 1.2f);
CGFloat selectionHeight = floorf(selectionWidth * 0.75f);
return CGSizeMake(selectionWidth, selectionHeight);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// The amount of space on either side of the scrubber's left and right edges.
- (CGFloat)horizontalMargins {
CGSize photoSize = [self photoSize];
return floorf(photoSize.width / 2);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (CGFloat)spaceBetweenPhotos {
return 1;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// The maximum number of pixels that the scrubber can utilize. The scrubber layer's border
// is contained within this width and must be considered when laying out the thumbnails.
- (CGFloat)maxContentWidth {
CGSize boundsSize = self.bounds.size;
CGFloat horizontalMargins = [self horizontalMargins];
CGFloat maxContentWidth = (boundsSize.width
- horizontalMargins * 2);
return maxContentWidth;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSInteger)numberOfVisiblePhotos {
CGSize photoSize = [self photoSize];
CGFloat spaceBetweenPhotos = [self spaceBetweenPhotos];
// Here's where we take into account the container layer's border because we don't want to
// display thumbnails on top of the border.
CGFloat maxContentWidth = ([self maxContentWidth]
- _containerView.layer.borderWidth * 2);
NSInteger numberOfPhotosThatFit = (NSInteger)floor((maxContentWidth + spaceBetweenPhotos)
/ (photoSize.width + spaceBetweenPhotos));
return MIN(_numberOfPhotos, numberOfPhotosThatFit);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (CGRect)frameForSelectionAtIndex:(NSInteger)photoIndex {
CGSize photoSize = [self photoSize];
CGSize selectionSize = [self selectionSize];
CGFloat containerWidth = _containerView.bounds.size.width;
// TODO (jverkoey July 21, 2011): I need to figure out why this is necessary.
// Basically, when there are a lot of photos it seems like the selection frame
// slowly gets offset from the thumbnail frame it's supposed to be representing until by the end
// it's off the right edge by a noticeable amount. Trimming off some fat from the right
// edge seems to fix this.
if (_numberOfVisiblePhotos < _numberOfPhotos) {
containerWidth -= photoSize.width / 2;
}
// Calculate the offset into the container view based on index/numberOfPhotos.
CGFloat relativeOffset = floorf((((CGFloat)photoIndex * containerWidth)
/ (CGFloat)MAX(1, _numberOfPhotos)));
return CGRectMake(floorf(_containerView.frame.origin.x
+ relativeOffset
+ photoSize.width / 2 - selectionSize.width / 2),
floorf(_containerView.center.y - selectionSize.height / 2),
selectionSize.width, selectionSize.height);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (CGRect)frameForThumbAtIndex:(NSInteger)thumbIndex {
CGSize photoSize = [self photoSize];
CGFloat spaceBetweenPhotos = [self spaceBetweenPhotos];
return CGRectMake(_containerView.layer.borderWidth
+ (photoSize.width + spaceBetweenPhotos) * thumbIndex,
_containerView.layer.borderWidth,
photoSize.width, photoSize.height);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)layoutSubviews {
[super layoutSubviews];
CGSize boundsSize = self.bounds.size;
CGSize photoSize = [self photoSize];
CGFloat spaceBetweenPhotos = [self spaceBetweenPhotos];
CGFloat maxContentWidth = [self maxContentWidth];
// Update the total number of visible photos.
_numberOfVisiblePhotos = [self numberOfVisiblePhotos];
// Hide views if there isn't any interesting information to show.
_containerView.hidden = (0 == _numberOfVisiblePhotos);
_selectionView.hidden = (_selectedPhotoIndex < 0 || _containerView.hidden);
// Calculate the container width using the number of visible photos.
CGFloat containerWidth = ((_numberOfVisiblePhotos * photoSize.width)
+ (MAX(0, _numberOfVisiblePhotos - 1) * spaceBetweenPhotos)
+ _containerView.layer.borderWidth * 2);
// Then we center the container in the content area.
CGFloat containerMargins = MAX(0, floorf((maxContentWidth - containerWidth) / 2));
CGFloat horizontalMargins = [self horizontalMargins];
CGFloat containerHeight = photoSize.height + _containerView.layer.borderWidth * 2;
CGFloat containerLeftMargin = horizontalMargins + containerMargins;
CGFloat containerTopMargin = floorf((boundsSize.height - containerHeight) / 2);
_containerView.frame = CGRectMake(containerLeftMargin,
containerTopMargin,
containerWidth,
containerHeight);
// Don't bother updating the selected photo index if there isn't a selection; the
// selection view will be hidden anyway.
if (_selectedPhotoIndex >= 0) {
_selectionView.frame = [self frameForSelectionAtIndex:_selectedPhotoIndex];
}
// Update the frames for all of the thumbnails.
[self updateVisiblePhotos];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Transforms an index into the number of visible photos into an index into the total
// number of photos.
- (NSInteger)photoIndexAtScrubberIndex:(NSInteger)scrubberIndex {
return (NSInteger)(ceilf((CGFloat)(scrubberIndex * _numberOfPhotos)
/ (CGFloat)_numberOfVisiblePhotos)
+ 0.5f);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)updateVisiblePhotos {
if (nil == self.dataSource) {
return;
}
// This will update the number of visible photos if the layout did indeed change.
[self layoutIfNeeded];
// Recycle any views that we no longer need.
while ([_visiblePhotoViews count] > _numberOfVisiblePhotos) {
UIView* photoView = [_visiblePhotoViews lastObject];
[photoView removeFromSuperview];
[_recycledPhotoViews addObject:photoView];
[_visiblePhotoViews removeLastObject];
}
// Lay out the visible photos.
for (NSInteger ix = 0; ix < _numberOfVisiblePhotos; ++ix) {
UIImageView* photoView = nil;
// We must first get the photo view at this index.
// If there aren't enough visible photo views then try to recycle another view.
if (ix >= [_visiblePhotoViews count]) {
photoView = [[[_recycledPhotoViews anyObject] retain] autorelease];
if (nil == photoView) {
// Couldn't recycle the view, so create a new one.
photoView = [self photoView];
} else {
[_recycledPhotoViews removeObject:photoView];
}
[_containerView addSubview:photoView];
[_visiblePhotoViews addObject:photoView];
} else {
photoView = [_visiblePhotoViews objectAtIndex:ix];
}
NSInteger photoIndex = [self photoIndexAtScrubberIndex:ix];
// Only request the thumbnail if this thumbnail's photo index has changed. Otherwise
// we assume that this photo either already has the thumbnail or it's still loading.
if (photoView.tag != photoIndex) {
photoView.tag = photoIndex;
photoView.image = [self.dataSource photoScrubberView:self thumbnailAtIndex:photoIndex];
}
photoView.frame = [self frameForThumbAtIndex:ix];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Changing Selection
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSInteger)photoIndexAtPoint:(CGPoint)point {
NSInteger photoIndex;
if (point.x <= 0) {
// Beyond the left edge
photoIndex = 0;
} else if (point.x >= _containerView.bounds.size.width) {
// Beyond the right edge
photoIndex = (_numberOfPhotos - 1);
} else {
// Somewhere in between
photoIndex = (NSInteger)(floorf((point.x / _containerView.bounds.size.width) * _numberOfPhotos)
+ 0.5f);
}
return photoIndex;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)updateSelectionWithPoint:(CGPoint)point {
NSInteger photoIndex = [self photoIndexAtPoint:point];
if (photoIndex != _selectedPhotoIndex) {
[self setSelectedPhotoIndex:photoIndex];
if ([self.delegate respondsToSelector:@selector(photoScrubberViewDidChangeSelection:)]) {
[self.delegate photoScrubberViewDidChangeSelection:self];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark UIResponder
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch* touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:_containerView];
[self updateSelectionWithPoint:touchPoint];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
UITouch* touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:_containerView];
[self updateSelectionWithPoint:touchPoint];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Public Methods
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didLoadThumbnail: (UIImage *)image
atIndex: (NSInteger)photoIndex {
for (UIImageView* thumbView in _visiblePhotoViews) {
if (thumbView.tag == photoIndex) {
thumbView.image = image;
break;
}
}
// Update the selected thumbnail if it's the one that just received a photo.
if (_selectedPhotoIndex == photoIndex) {
_selectionView.image = image;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)reloadData {
NIDASSERT(nil != _dataSource);
// Remove any visible photos from the view before we release the sets.
for (UIView* photoView in _visiblePhotoViews) {
[photoView removeFromSuperview];
}
NI_RELEASE_SAFELY(_visiblePhotoViews);
NI_RELEASE_SAFELY(_recycledPhotoViews);
// If there is no data source then we can't do anything particularly interesting.
if (nil == _dataSource) {
return;
}
_visiblePhotoViews = [[NSMutableArray alloc] init];
_recycledPhotoViews = [[NSMutableSet alloc] init];
// Cache the number of photos.
_numberOfPhotos = [_dataSource numberOfPhotosInScrubberView:self];
[self setNeedsLayout];
// This will call layoutIfNeeded and layoutSubviews will then be called because we
// set the needsLayout flag.
[self updateVisiblePhotos];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setSelectedPhotoIndex:(NSInteger)photoIndex animated:(BOOL)animated {
if (_selectedPhotoIndex != photoIndex) {
// Don't animate the selection if it was previously invalid.
animated = animated && (_selectedPhotoIndex >= 0);
_selectedPhotoIndex = photoIndex;
if (animated) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationBeginsFromCurrentState:YES];
}
_selectionView.frame = [self frameForSelectionAtIndex:_selectedPhotoIndex];
if (animated) {
[UIView commitAnimations];
}
_selectionView.image = [self.dataSource photoScrubberView: self
thumbnailAtIndex: _selectedPhotoIndex];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setSelectedPhotoIndex:(NSInteger)photoIndex {
[self setSelectedPhotoIndex:photoIndex animated:NO];
}
@end