-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFBMediaViewerView.m
More file actions
622 lines (495 loc) · 17.9 KB
/
FBMediaViewerView.m
File metadata and controls
622 lines (495 loc) · 17.9 KB
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
//
// Copyright (c) 2012 Five-technology Co.,Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import <QuartzCore/QuartzCore.h>
#import "FBMediaViewerView.h"
#import "FBMediaViewerInnerView.h"
#import "FBMediaViewerItemLoader.h"
#define FB_MEDIA_VIEWER_VIEW_DEFAULT_SPACING_WIDTH 40
#define FB_MEDIA_VIEWER_VIEW_DEFAULT_SPACING_HEIGHT 0
#define FB_MEDIA_VIEWER_VIEW_DEFAULT_MARGIN_HEIGHT 50
#define FB_MEDIA_VIEWER_VIEW_DEFAULT_MARGIN_WIDTH_RATE 0.2
#define FB_MEDIA_VIEWER_VIEW_DEFAULT_TRANSITION_DURATION 0.75
#define FB_MEDIA_VIEWER_VIEW_NUMBER_OF_INNER_VIEW 3
#define FB_MEDIA_VIEWER_VIEW_LENGTH_FROM_CENTER ((FB_MEDIA_VIEWER_VIEW_NUMBER_OF_INNER_VIEW-1)/2)
#define FB_MEDIA_VIEWER_VIEW_INDEX_OF_CURRENT_INNER_VIEW ((FB_MEDIA_VIEWER_VIEW_NUMBER_OF_INNER_VIEW-1)/2)
#pragma Properties (Private)
@interface FBMediaViewerView()
@property (nonatomic, strong) UIScrollView* baseScrollView;
@property (nonatomic, assign) NSInteger contentOffsetIndex;
@property (nonatomic, strong) NSMutableArray* innerViews;
@property (nonatomic, assign) CGSize spacing;
@property (nonatomic, assign) BOOL animatingWithScrolling;
@property (nonatomic, assign) CGSize previousFrameSize;
@property (nonatomic, strong) UIPageControl* pageControl;
@property (nonatomic, strong) FBMediaViewerInnerView* transitionInnerView;
@end
@implementation FBMediaViewerView
#pragma mark -
#pragma mark Privates
- (NSInteger)_numberOfItems
{
NSInteger numberOfItems = [self.dataSource numberOfItemsInMediaViewerView:self];
if (numberOfItems < 0) {
numberOfItems = 0;
}
return numberOfItems;
}
- (void)_setItemAtIndex:(NSInteger)index toInnerView:(FBMediaViewerInnerView*)innerView
{
if (index < 0 || [self _numberOfItems] <= index) {
innerView.mediaViewerItem = nil;
return;
}
innerView.mediaViewerItem = [self.dataSource mediaViewerView:self itemAtIndex:index];
}
- (CGSize)_unitSize
{
CGSize size = self.bounds.size;
size.width += self.spacing.width;
return size;
}
- (void)_layoutBaseScrollView
{
CGRect scrollViewFrame = self.bounds;
scrollViewFrame.origin.x -= self.spacing.width/2.0;
scrollViewFrame.size.width += self.spacing.width;
self.baseScrollView.frame =scrollViewFrame;
}
- (void)_layoutInnerViews
{
CGRect innerViewFrame = CGRectZero;
innerViewFrame.size = self.bounds.size;
innerViewFrame.origin.x = (self.contentOffsetIndex-FB_MEDIA_VIEWER_VIEW_LENGTH_FROM_CENTER) * innerViewFrame.size.width;
for (int index=0; index < FB_MEDIA_VIEWER_VIEW_NUMBER_OF_INNER_VIEW; index++) {
FBMediaViewerInnerView* innerView = [self.innerViews objectAtIndex:index];
innerViewFrame.origin.x += self.spacing.width/2.0; // left space
innerView.frame = innerViewFrame;
innerViewFrame.origin.x += innerViewFrame.size.width; // next
innerViewFrame.origin.x += self.spacing.width/2.0; // right space
}
}
- (void)_layoutViewsAnimated:(BOOL)animated
{
if (animated) {
[UIView beginAnimations:nil context:nil];
}
[self _layoutBaseScrollView];
[self _layoutInnerViews];
if (animated) {
[UIView commitAnimations];
}
}
- (FBMediaViewerInnerView*)_innerViewWithFrame:(CGRect)innerViewFrame
{
FBMediaViewerInnerView* innerView =
[[FBMediaViewerInnerView alloc] initWithMediaViewerView:self
frame:innerViewFrame];
innerView.clipsToBounds = YES;
innerView.backgroundColor = self.backgroundColor;
return innerView;
}
- (void)_setupSubViews
{
// initialize vars
self.spacing = CGSizeMake(
FB_MEDIA_VIEWER_VIEW_DEFAULT_SPACING_WIDTH, FB_MEDIA_VIEWER_VIEW_DEFAULT_SPACING_HEIGHT);
self.baseScrollView.clipsToBounds = NO;
// setup self view
//-------------------------
// self.autoresizingMask =
// UIViewAutoresizingFlexibleLeftMargin |
// UIViewAutoresizingFlexibleWidth |
// UIViewAutoresizingFlexibleRightMargin |
// UIViewAutoresizingFlexibleTopMargin |
// UIViewAutoresizingFlexibleHeight |
// UIViewAutoresizingFlexibleBottomMargin;
self.clipsToBounds = YES;
// setup base scroll view
//-------------------------
self.baseScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
// self.baseScrollView.delegate = self; ==> move other place
self.baseScrollView.pagingEnabled = YES;
self.baseScrollView.showsHorizontalScrollIndicator = NO;
self.baseScrollView.showsVerticalScrollIndicator = NO;
self.baseScrollView.scrollsToTop = NO;
self.baseScrollView.autoresizingMask =
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
self.baseScrollView.backgroundColor = self.backgroundColor;
[self _layoutBaseScrollView];
[self addSubview:self.baseScrollView];
// setup internal scroll views
//------------------------------
CGRect innerViewFrame = CGRectZero;
// innerViewFrame.size = [self baseFrame].size;
self.innerViews = [NSMutableArray array];
for (int i=0; i < FB_MEDIA_VIEWER_VIEW_NUMBER_OF_INNER_VIEW; i++) {
FBMediaViewerInnerView* innerView = [self _innerViewWithFrame:innerViewFrame];
[self.baseScrollView addSubview:innerView];
[self.innerViews addObject:innerView];
}
[self _layoutInnerViews];
// setup temporary view for slideshow transition
self.transitionInnerView = [self _innerViewWithFrame:innerViewFrame];
self.transitionInnerView.hidden = YES;
[self.baseScrollView addSubview:self.transitionInnerView];
// setup page control
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectZero];
self.pageControl.autoresizingMask =
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleBottomMargin;
self.pageControl.hidesForSinglePage = NO;
[self.pageControl addTarget:self
action:@selector(_pageControlDidChange:)
forControlEvents:UIControlEventValueChanged];
[self addSubview:self.pageControl];
// re-layout
self.pageControlPosition = FBMediaViewerViewPageControllerPositionTop;
}
- (void)_layoutSubviewsWithSizeChecking:(BOOL)checking animated:(BOOL)animated
{
CGSize newSize = self.bounds.size;
CGSize oldSize = self.previousFrameSize;
if (checking && CGSizeEqualToSize(newSize, oldSize)) {
return;
}
self.previousFrameSize = newSize;
CGSize newSizeWithSpace = newSize;
newSizeWithSpace.width += self.spacing.width;
// set new origin and size to innerViews
CGFloat x = (self.contentOffsetIndex-FB_MEDIA_VIEWER_VIEW_LENGTH_FROM_CENTER) * newSizeWithSpace.width;
for (FBMediaViewerInnerView* innerView in self.innerViews) {
x += self.spacing.width/2.0; // left space
innerView.frame = CGRectMake(x, 0, newSize.width, newSize.height);
x += newSize.width;
x += self.spacing.width/2.0; // right space
}
// adjust content size and offset of base scrollView
//--
self.baseScrollView.contentSize = CGSizeMake(
[self _numberOfItems]*newSizeWithSpace.width,
newSize.height);
[self.baseScrollView setContentOffset:CGPointMake(
self.contentOffsetIndex*newSizeWithSpace.width, 0)
animated:animated];
[self._currentInnerView willAppear];
}
- (FBMediaViewerInnerView*)_currentInnerView
{
return [self.innerViews objectAtIndex:FB_MEDIA_VIEWER_VIEW_INDEX_OF_CURRENT_INNER_VIEW];
}
-(void)_pageControlDidChange:(id)sender
{
[self moveToIndex:self.pageControl.currentPage animated:YES];
}
#pragma mark -
#pragma mark Basics
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder])) {
[self _setupSubViews];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.backgroundColor = [UIColor blackColor];
[self _setupSubViews];
}
return self;
}
- (void)dealloc
{
// cleanup
[self.itemLoader cancelAllItems];
// public
self.dataSource = nil;
self.delegate = nil;
self.itemLoader = nil;
// private
self.baseScrollView = nil;
self.innerViews = nil;
}
- (void)layoutSubviews
{
[self _layoutSubviewsWithSizeChecking:YES animated:NO];
if (self.baseScrollView.delegate == nil) {
self.baseScrollView.delegate = self;
}
}
#pragma mark -
#pragma mark Privates (Control Scroll)
-(void)_scrollToLeft
{
FBMediaViewerInnerView* rightView =
[self.innerViews objectAtIndex:FB_MEDIA_VIEWER_VIEW_NUMBER_OF_INNER_VIEW-1];
FBMediaViewerInnerView* leftView = [self.innerViews objectAtIndex:0];
CGRect frame = leftView.frame;
frame.origin.x -= frame.size.width + self.spacing.width;
rightView.frame = frame;
[self.innerViews removeObjectAtIndex:FB_MEDIA_VIEWER_VIEW_NUMBER_OF_INNER_VIEW-1];
[self.innerViews insertObject:rightView atIndex:0];
[self _setItemAtIndex:self.currentIndex-FB_MEDIA_VIEWER_VIEW_LENGTH_FROM_CENTER
toInnerView:rightView];
}
-(void)_scrollToRight
{
FBMediaViewerInnerView* rightView =
[self.innerViews objectAtIndex:FB_MEDIA_VIEWER_VIEW_NUMBER_OF_INNER_VIEW-1];
FBMediaViewerInnerView* leftView = [self.innerViews objectAtIndex:0];
CGRect frame = rightView.frame;
frame.origin.x += frame.size.width + self.spacing.width;
leftView.frame = frame;
[self.innerViews removeObjectAtIndex:0];
[self.innerViews addObject:leftView];
[self _setItemAtIndex:self.currentIndex+FB_MEDIA_VIEWER_VIEW_LENGTH_FROM_CENTER
toInnerView:leftView];
}
- (void)_movePage:(BOOL)animated
{
self.animatingWithScrolling = YES;
[self.baseScrollView setContentOffset:CGPointMake(
self.contentOffsetIndex*[self _unitSize].width, 0)
animated:animated];
}
#pragma mark -
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (!scrollView.dragging) {
return;
}
CGFloat position = scrollView.contentOffset.x / scrollView.bounds.size.width;
CGFloat delta = position - (CGFloat)self.currentIndex;
if (fabs(delta) >= 1.0f) {
if ([self.delegate respondsToSelector:@selector(mediaViewerView:willMoveFromIndex:)]) {
[self.delegate mediaViewerView:self willMoveFromIndex:self.currentIndex];
}
if (delta > 0) {
if (self.currentIndex < self._numberOfItems-1) {
[self._currentInnerView willDisAppear];
self.currentIndex++;
self.contentOffsetIndex++;
[self _scrollToRight];
[self._currentInnerView willAppear];
self.pageControl.currentPage = self.currentIndex;
}
} else {
if (self.currentIndex >= 1) {
[self._currentInnerView willDisAppear];
self.currentIndex--;
self.contentOffsetIndex--;
[self _scrollToLeft];
[self._currentInnerView willAppear];
self.pageControl.currentPage = self.currentIndex;
}
}
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
self.animatingWithScrolling = NO;
}
#pragma mark -
#pragma mark Properties
- (void)setCurrentIndex:(NSInteger)currentIndex
{
_currentIndex = currentIndex;
if ([self.delegate respondsToSelector:@selector(mediaViewerView:didMoveToIndex:)]) {
[self.delegate mediaViewerView:self didMoveToIndex:currentIndex];
}
}
- (void)_setPageControlHidden:(BOOL)hidden
{
CGFloat alpha = hidden ? 0.0 : 1.0;
[UIView animateWithDuration:0.2
animations:^{
self.pageControl.alpha = alpha;
}];
}
- (void)_refreshPageControlPostion
{
CGFloat y;
switch (_pageControlPosition) {
case FBMediaViewerViewPageControllerPositionTop:
y = 0;
break;
case FBMediaViewerViewPageControllerPositionBottom:
default:
y = self.bounds.size.height-FB_MEDIA_VIEWER_VIEW_DEFAULT_MARGIN_HEIGHT;
break;
}
self.pageControl.frame = CGRectMake(0, y + self.pageControlOffset, self.bounds.size.width, FB_MEDIA_VIEWER_VIEW_DEFAULT_MARGIN_HEIGHT);
}
- (void)setPageControlPosition:(FBMediaViewerViewPageControllerPosition)pageControlPosition
{
_pageControlPosition = pageControlPosition;
[self _refreshPageControlPostion];
}
- (void)setPageControlOffset:(CGFloat)pageControlOffset
{
_pageControlOffset = pageControlOffset;
[self _refreshPageControlPostion];
}
- (void)setPageControlHidden:(BOOL)hidden
{
_pageControlHidden = hidden;
if ([self _numberOfItems] <= 1) {
[self _setPageControlHidden:YES];
} else {
[self _setPageControlHidden:hidden];
}
}
#pragma mark -
#pragma mark API
- (void)reloadData
{
NSInteger numberOfItems = [self _numberOfItems];
if (self.currentIndex >= numberOfItems) {
if (numberOfItems == 0) {
self.currentIndex = 0;
} else {
self.currentIndex = numberOfItems-1;
}
}
for (int index=0; index < FB_MEDIA_VIEWER_VIEW_NUMBER_OF_INNER_VIEW; index++) {
[self _setItemAtIndex:self.currentIndex + index - FB_MEDIA_VIEWER_VIEW_LENGTH_FROM_CENTER
toInnerView:[self.innerViews objectAtIndex:index]];
}
self.pageControl.numberOfPages = numberOfItems;
self.pageControl.currentPage = self.currentIndex;
if (numberOfItems <= 1) {
[self _setPageControlHidden:YES];
} else {
[self _setPageControlHidden:self.pageControlHidden];
}
}
- (void)reloadCurrentItemWithMode:(FBMeditViewerItemLoaderMode)mode
{
[[self _currentInnerView] loadWithMode:mode];
}
- (void)moveToIndex:(NSInteger)index animated:(BOOL)animated
{
NSInteger numberOfItems = [self _numberOfItems];
if (numberOfItems == 0) {
return;
}
if (index < 0) {
index = 0;
} else if (index >= numberOfItems) {
index = numberOfItems - 1;
}
self.currentIndex = index;
self.contentOffsetIndex = index;
self.pageControl.currentPage = index;
for (int i=0; i < FB_MEDIA_VIEWER_VIEW_NUMBER_OF_INNER_VIEW; i++) {
[self _setItemAtIndex:self.currentIndex + i - FB_MEDIA_VIEWER_VIEW_LENGTH_FROM_CENTER
toInnerView:[self.innerViews objectAtIndex:i]];
}
[self _layoutViewsAnimated:NO];
[self _layoutSubviewsWithSizeChecking:NO animated:animated];
}
- (void)moveToPreviousItemAnimated:(BOOL)animated
{
if (self.animatingWithScrolling || self.currentIndex <= 0) {
// do nothing
return;
}
self.currentIndex--;
self.contentOffsetIndex--;
self.pageControl.currentPage--;
[self _scrollToLeft];
[self _movePage:animated];
}
- (void)moveToNextItemAnimated:(BOOL)animated
{
if (self.animatingWithScrolling || self.currentIndex >= [self _numberOfItems]-1) {
// do nothing
return;
}
self.currentIndex++;
self.contentOffsetIndex++;
self.pageControl.currentPage++;
[self _scrollToRight];
[self _movePage:animated];
}
- (void)moveToLastItemAnimated:(BOOL)animated
{
[self moveToIndex:9999999 animated:animated];
}
- (id <FBMediaViewerItem>)currentItem
{
return self._currentInnerView.mediaViewerItem;
}
- (void)removeCurrentIndexAimated:(BOOL)animated
{
NSInteger numberOfItems = [self _numberOfItems];
NSInteger transitionIndex = self.currentIndex;
if (numberOfItems == 0) {
transitionIndex = -1;
} else if (numberOfItems == self.currentIndex) {
transitionIndex--;
} else {
}
// [1] setup transitionView
[self _setItemAtIndex:transitionIndex
toInnerView:self.transitionInnerView];
FBMediaViewerInnerView* currentInnerScrollView =
[self.innerViews objectAtIndex:FB_MEDIA_VIEWER_VIEW_INDEX_OF_CURRENT_INNER_VIEW];
self.transitionInnerView.frame = currentInnerScrollView.frame;
// [2] do transition
if (animated) {
CATransition* transition = [CATransition animation];
transition.duration = FB_MEDIA_VIEWER_VIEW_DEFAULT_TRANSITION_DURATION;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;
// transition.delegate = self;
[self.baseScrollView.layer addAnimation:transition forKey:nil];
}
currentInnerScrollView.hidden = YES;
self.transitionInnerView.hidden = NO;
[self.innerViews replaceObjectAtIndex:FB_MEDIA_VIEWER_VIEW_INDEX_OF_CURRENT_INNER_VIEW
withObject:self.transitionInnerView];
self.transitionInnerView = currentInnerScrollView;
// [3] re-layout subviews
[self _layoutSubviewsWithSizeChecking:NO animated:NO];
// [4] notify
if (numberOfItems &&
[self.delegate respondsToSelector:@selector(mediaViewerView:didMoveToIndex:)]) {
[self.delegate mediaViewerView:self didMoveToIndex:self.currentIndex];
}
}
#pragma mark -
#pragma mark API (IBActions)
- (IBAction)moveToPreviousItem:(id)sender
{
[self moveToPreviousItemAnimated:YES];
}
- (IBAction)moveToNextItem:(id)sender
{
[self moveToNextItemAnimated:YES];
}
@end