forked from facebookarchive/three20
/
TTScrollView.m
1135 lines (965 loc) · 35.1 KB
/
TTScrollView.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
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "Three20/TTScrollView.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
static const NSInteger kOffscreenPages = 1;
static const CGFloat kDefaultPageSpacing = 40.0;
static const CGFloat kFlickThreshold = 60.0;
static const CGFloat kTapZoom = 0.75;
static const NSInteger kInvalidIndex = -1;
static const NSTimeInterval kFlickDuration = 0.4;
static const NSTimeInterval kBounceDuration = 0.5;
static const NSTimeInterval kOvershoot = 2;
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation TTScrollView
@synthesize delegate = _delegate, dataSource = _dataSource, centerPageIndex = _centerPageIndex,
pageSpacing = _pageSpacing, scrollEnabled = _scrollEnabled, zoomEnabled = _zoomEnabled,
rotateEnabled = _rotateEnabled, orientation = _orientation;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.clipsToBounds = YES;
self.multipleTouchEnabled = YES;
self.userInteractionEnabled = YES;
_delegate = nil;
_dataSource = nil;
_maxPages = (kOffscreenPages*2) + 1;
_pages = [[NSMutableArray alloc] initWithCapacity:_maxPages];
_pageQueue = [[NSMutableArray alloc] init];
_pageSpacing = kDefaultPageSpacing;
_centerPageIndex = 0;
_visiblePageIndex = kInvalidIndex;
_pageArrayIndex = 0;
_touchCount = 0;
_pageEdges = UIEdgeInsetsZero;
_pageStartEdges = UIEdgeInsetsZero;
_touchEdges = UIEdgeInsetsZero;
_touchStartEdges = UIEdgeInsetsZero;
_scrollEnabled = YES;
_zoomEnabled = YES;
_rotateEnabled = YES;
_orientation = TTDeviceOrientation();
_tapTimer = nil;
_animationTimer = nil;
_touch1 = nil;
_touch2 = nil;
_dragging = NO;
_zooming = NO;
_overshoot = 0;
for (NSInteger i = 0; i < _maxPages; ++i) {
[_pages addObject:[NSNull null]];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceOrientationDidChange:)
name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"UIDeviceOrientationDidChangeNotification" object:nil];
_delegate = nil;
[_animationTimer invalidate];
[_tapTimer invalidate];
[_animationStartTime release];
[_pages release];
[_pageQueue release];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)isFirstPage {
return _centerPageIndex == 0;
}
- (BOOL)isLastPage {
return _centerPageIndex+1 >= [_dataSource numberOfPagesInScrollView:self];
}
- (BOOL)draggingFromEdge {
return (_pageEdges.left < 0 && [self isLastPage]) || (_pageEdges.left > 0 && [self isFirstPage]);
}
- (BOOL)flipped {
return _orientation == UIInterfaceOrientationLandscapeLeft
|| _orientation == UIInterfaceOrientationPortraitUpsideDown;
}
- (BOOL)pinched {
return -_pageEdges.left + _pageEdges.right < 0;
}
- (BOOL)pulled {
return _pageEdges.left > 0 || _pageEdges.top > 0 || _pageEdges.right < 0 || _pageEdges.bottom < 0;
}
- (BOOL)zoomed {
return _pageEdges.left != _pageEdges.right || _pageEdges.top != _pageEdges.bottom;
}
- (BOOL)flicked {
if (!self.flipped) {
if (_pageEdges.left > kFlickThreshold && ![self isFirstPage]) {
return YES;
} else if (_pageEdges.right < -kFlickThreshold && ![self isLastPage]) {
return YES;
} else {
return NO;
}
} else {
if (_pageEdges.left > kFlickThreshold && ![self isLastPage]) {
return YES;
} else if (_pageEdges.right < -kFlickThreshold && ![self isFirstPage]) {
return YES;
} else {
return NO;
}
}
}
- (CGFloat)pageWidth {
if (UIInterfaceOrientationIsLandscape(_orientation)) {
return self.height;
} else {
return self.width;
}
}
- (CGFloat)pageHeight {
if (UIInterfaceOrientationIsLandscape(_orientation)) {
return self.width;
} else {
return self.height;
}
}
- (CGFloat)overshoot {
return _pageEdges.left < 0 ? -_overshoot : _overshoot;
}
- (CGFloat)zoomFactor {
CGFloat stretchedWidth = -_pageEdges.left + self.pageWidth + _pageEdges.right;
return stretchedWidth / self.pageWidth;
}
- (CGRect)frameOfPageAtIndex:(NSInteger)pageIndex {
CGSize size;
if ([_dataSource respondsToSelector:@selector(scrollView:sizeOfPageAtIndex:)]) {
size = [_dataSource scrollView:self sizeOfPageAtIndex:pageIndex];
if (!size.width || !size.height) {
size = CGSizeMake(self.pageWidth, self.pageHeight);
}
} else {
size = CGSizeMake(self.pageWidth, self.pageHeight);
}
CGFloat width, height;
if (UIInterfaceOrientationIsLandscape(_orientation)) {
if (size.width > size.height) {
height = size.width/size.height * self.width;
width = self.width;
} else {
height = size.width/size.height * self.width;
width = self.width;
}
} else {
if (size.width > size.height) {
width = self.width;
height = size.height/size.width * self.width;
} else {
width = size.width/size.height * self.height;
height = self.height;
}
}
CGFloat xd = width - self.width;
CGFloat yd = height - self.height;
return CGRectMake(-xd/2, -yd/2, width, height);
}
- (CGFloat)overflowForFrame:(CGRect)frame {
if (UIInterfaceOrientationIsLandscape(_orientation)) {
return frame.origin.y < 0 ? fabs(frame.origin.y) : 0;
} else {
return frame.origin.x < 0 ? fabs(frame.origin.x) : 0;
}
}
- (CGPoint)offsetForOrientation:(CGFloat)x y:(CGFloat)y {
if (UIInterfaceOrientationIsLandscape(_orientation)) {
return CGPointMake(y, x);
} else {
return CGPointMake(x, y);
}
}
- (BOOL)supportsOrientation:(UIInterfaceOrientation)orientation {
return orientation == UIInterfaceOrientationLandscapeLeft
|| orientation == UIInterfaceOrientationLandscapeRight
|| orientation == UIInterfaceOrientationPortrait
|| orientation == UIInterfaceOrientationPortraitUpsideDown;
}
- (CGAffineTransform)rotateTransform:(CGAffineTransform)transform {
if (_orientation == UIInterfaceOrientationLandscapeLeft) {
return CGAffineTransformRotate(transform, M_PI*1.5);
} else if (_orientation == UIInterfaceOrientationLandscapeRight) {
return CGAffineTransformRotate(transform, M_PI/2);
} else if (_orientation == UIInterfaceOrientationPortraitUpsideDown) {
return CGAffineTransformRotate(transform, -M_PI);
} else {
return transform;
}
}
- (CGAffineTransform)transformForOrientation:(UIInterfaceOrientation)orientation {
if (orientation == UIInterfaceOrientationLandscapeLeft) {
return CGAffineTransformMakeRotation(M_PI*1.5);
} else if (orientation == UIInterfaceOrientationLandscapeRight) {
return CGAffineTransformMakeRotation(M_PI/2);
} else if (_orientation == UIInterfaceOrientationPortraitUpsideDown) {
return CGAffineTransformMakeRotation(-M_PI);
} else {
return CGAffineTransformIdentity;
}
}
- (CGPoint)touchLocation:(UITouch*)touch {
CGPoint point = [touch locationInView:self];
if (UIInterfaceOrientationIsLandscape(_orientation)) {
return CGPointMake(point.y, point.x);
} else {
return point;
}
}
- (NSInteger)arrayIndexForPageIndex:(NSInteger)pageIndex relativeToIndex:(NSInteger)baseIndex {
NSInteger numberOfPages = self.numberOfPages;
if (!numberOfPages || pageIndex >= numberOfPages || pageIndex < 0) {
return kInvalidIndex;
}
NSInteger indexDiff = pageIndex - baseIndex;
if (fabs(indexDiff) > kOffscreenPages) {
return kInvalidIndex;
}
NSInteger arrayIndex = _pageArrayIndex + indexDiff;
if (arrayIndex >= _maxPages) {
return arrayIndex - _maxPages;
} else if (arrayIndex < 0) {
return _maxPages + arrayIndex;
} else {
return arrayIndex;
}
}
- (NSInteger)realPageIndex {
if (self.pinched) {
return _centerPageIndex;
} else if (!self.flipped) {
if (_pageEdges.left > kFlickThreshold && ![self isFirstPage]) {
return _centerPageIndex - 1;
} else if (_pageEdges.right < -kFlickThreshold && ![self isLastPage]) {
return _centerPageIndex + 1;
} else {
return _centerPageIndex;
}
} else {
if (_pageEdges.left > kFlickThreshold && ![self isLastPage]) {
return _centerPageIndex + 1;
} else if (_pageEdges.right < -kFlickThreshold && ![self isFirstPage]) {
return _centerPageIndex - 1;
} else {
return _centerPageIndex;
}
}
}
- (UIView*)pageAtIndex:(NSInteger)pageIndex create:(BOOL)create {
NSInteger arrayIndex = [self arrayIndexForPageIndex:pageIndex relativeToIndex:_centerPageIndex];
if (arrayIndex == kInvalidIndex) {
return nil;
}
UIView* page = [_pages objectAtIndex:arrayIndex];
if ((NSNull*)page == [NSNull null]) {
if (create) {
page = [_dataSource scrollView:self pageAtIndex:pageIndex];
page.multipleTouchEnabled = YES;
page.userInteractionEnabled = YES;
[self addSubview:page];
[_pages replaceObjectAtIndex:arrayIndex withObject:page];
} else {
return nil;
}
}
return page;
}
- (UIView*)enqueuePageAtIndex:(NSInteger)pageIndex {
NSInteger arrayIndex = [self arrayIndexForPageIndex:pageIndex relativeToIndex:_centerPageIndex];
if (arrayIndex == kInvalidIndex) {
return nil;
}
UIView* page = [_pages objectAtIndex:arrayIndex];
if ((NSNull*)page == [NSNull null]) {
return nil;
} else {
[_pageQueue addObject:page];
[_pages replaceObjectAtIndex:arrayIndex withObject:[NSNull null]];
[page removeFromSuperview];
}
return page;
}
- (void)enqueueAllPages {
for (NSInteger i = 0; i < _pages.count; ++i) {
UIView* page = [_pages objectAtIndex:i];
if ((NSNull*)page != [NSNull null]) {
[_pageQueue addObject:page];
[_pages replaceObjectAtIndex:i withObject:[NSNull null]];
[page removeFromSuperview];
}
}
}
- (void)adjustPageEdgesForPageAtIndex:(NSInteger)pageIndex {
CGRect centerFrame = [self frameOfPageAtIndex:_centerPageIndex];
CGFloat centerPageOverflow = [self overflowForFrame:centerFrame] * self.zoomFactor;
CGRect frame = [self frameOfPageAtIndex:pageIndex];
CGFloat overflow = [self overflowForFrame:frame];
if (self.flipped) {
CGFloat factor = _centerPageIndex > pageIndex ? -1 : 1;
CGFloat xd = (self.pageWidth + _pageSpacing + centerPageOverflow + overflow) * factor;
CGFloat left = _pageEdges.right > 0 ? _pageEdges.right : _pageEdges.left;
CGFloat right = _pageEdges.left < 0 ? _pageEdges.left : _pageEdges.right;
_pageEdges = _pageStartEdges = UIEdgeInsetsMake(0, left - xd, 0, right - xd);
} else {
CGFloat factor = _centerPageIndex > pageIndex ? 1 : -1;
CGFloat xd = (self.pageWidth + _pageSpacing + centerPageOverflow + overflow) * factor;
CGFloat left = _pageEdges.right < 0 ? _pageEdges.right : _pageEdges.left;
CGFloat right = _pageEdges.left > 0 ? _pageEdges.left : _pageEdges.right;
_pageEdges = _pageStartEdges = UIEdgeInsetsMake(0, right - xd, 0, left - xd);
}
}
- (void)moveToPageAtIndex:(NSInteger)pageIndex resetEdges:(BOOL)resetEdges {
if (resetEdges) {
_pageEdges = _pageStartEdges = UIEdgeInsetsZero;
_zooming = NO;
[self setNeedsLayout];
} else if (pageIndex != _centerPageIndex) {
[self adjustPageEdgesForPageAtIndex:pageIndex];
_zooming = NO;
}
NSInteger indexDiff = pageIndex - _centerPageIndex;
if (indexDiff) {
if (fabs(indexDiff) <= kOffscreenPages) {
if (indexDiff > 0) {
NSInteger edgeIndex = _centerPageIndex - kOffscreenPages;
NSInteger newEdgeIndex = pageIndex - kOffscreenPages;
for (int i = edgeIndex; i < newEdgeIndex; ++i) {
[self enqueuePageAtIndex:i];
}
} else if (indexDiff < 0) {
NSInteger edgeIndex = _centerPageIndex + kOffscreenPages;
NSInteger newEdgeIndex = pageIndex + kOffscreenPages;
for (int i = edgeIndex; i > newEdgeIndex; --i) {
[self enqueuePageAtIndex:i];
}
}
} else {
[self reloadData];
}
_pageArrayIndex = [self arrayIndexForPageIndex:pageIndex relativeToIndex:_centerPageIndex];
_centerPageIndex = pageIndex;
}
}
- (void)layoutPage {
UIView* page = [self pageAtIndex:_centerPageIndex create:YES];
if (page) {
CGAffineTransform rotation = [self transformForOrientation:_orientation];
CGPoint offset = [self offsetForOrientation:_pageEdges.left y:_pageEdges.top];
CGRect frame = [self frameOfPageAtIndex:_centerPageIndex];
if (self.zoomed) {
CGFloat zoom = self.zoomFactor;
page.transform = [self rotateTransform:CGAffineTransformScale(
CGAffineTransformMakeTranslation(offset.x, offset.y), zoom, zoom)];
page.frame = CGRectMake(offset.x + frame.origin.x*zoom, offset.y + frame.origin.y*zoom,
frame.size.width*zoom, frame.size.height*zoom);
} else {
page.transform = rotation;
page.frame = CGRectMake(offset.x + frame.origin.x, offset.y + frame.origin.y,
frame.size.width, frame.size.height);
}
}
}
- (void)layoutAdjacentPages {
BOOL flipped = self.flipped;
BOOL pinched = self.pinched;
CGAffineTransform rotation = [self transformForOrientation:_orientation];
NSInteger minPageIndex = _centerPageIndex - kOffscreenPages;
NSInteger maxPageIndex = _centerPageIndex + kOffscreenPages;
CGRect centerFrame = [self frameOfPageAtIndex:_centerPageIndex];
CGFloat centerPageOverflow = [self overflowForFrame:centerFrame] * self.zoomFactor;
CGFloat overflow = centerPageOverflow;
for (NSInteger i = _centerPageIndex - 1; i >= 0 && i >= minPageIndex; --i) {
UIView* page = [self pageAtIndex:i create:YES];
if (page) {
CGRect frame = [self frameOfPageAtIndex:i];
overflow += [self overflowForFrame:frame];
NSInteger relativeIndex = -(_centerPageIndex - i);
CGFloat x = flipped
? ((self.pageWidth + _pageSpacing) * -relativeIndex) + _pageEdges.right + overflow
: ((self.pageWidth + _pageSpacing) * relativeIndex) + _pageEdges.left - overflow;
CGPoint offset = [self offsetForOrientation:x y:0];
page.transform = rotation;
page.frame = CGRectMake(offset.x + frame.origin.x, offset.y + frame.origin.y,
frame.size.width, frame.size.height);
page.hidden = pinched;
}
}
overflow = centerPageOverflow;
NSInteger pageCount = [_dataSource numberOfPagesInScrollView:self];
for (NSInteger i = _centerPageIndex + 1; i < pageCount && i <= maxPageIndex; ++i) {
UIView* page = [self pageAtIndex:i create:YES];
if (page) {
CGRect frame = [self frameOfPageAtIndex:i];
overflow += [self overflowForFrame:frame];
NSInteger relativeIndex = i - _centerPageIndex;
CGFloat x = flipped
? ((self.pageWidth + _pageSpacing) * -relativeIndex) + _pageEdges.left - overflow
: ((self.pageWidth + _pageSpacing) * relativeIndex) + _pageEdges.right + overflow;
CGPoint offset = [self offsetForOrientation:x y:0];
page.transform = rotation;
page.frame = CGRectMake(offset.x + frame.origin.x, offset.y + frame.origin.y,
frame.size.width, frame.size.height);
page.hidden = pinched;
}
}
}
- (UIEdgeInsets)stretchTouchEdges:(UIEdgeInsets)edges toPoint:(CGPoint)point {
UIEdgeInsets newEdges = edges;
if (!edges.left || point.x < edges.left) {
newEdges.left = point.x;
}
if (!edges.right || point.x > edges.right) {
newEdges.right = point.x;
}
if (!edges.top || point.y < edges.top) {
newEdges.top = point.y;
}
if (!edges.bottom || point.y > edges.bottom) {
newEdges.bottom = point.y;
}
return newEdges;
}
- (UIEdgeInsets)squareTouchEdges:(UIEdgeInsets)edges {
if (_touchCount == 1) {
return edges;
} else {
CGFloat width = edges.right - edges.left;
CGFloat height = edges.bottom - edges.top;
CGFloat d = sqrt((width*width) + (height*height));
CGFloat midX = edges.left + (width/2);
CGFloat midY = edges.top + (height/2);
return UIEdgeInsetsMake(midY - d/2, midX - d/2, midY + d/2, midX + d/2);
}
}
- (UIEdgeInsets)touchEdgesForPoint:(CGPoint)point {
return [self stretchTouchEdges:UIEdgeInsetsZero toPoint:point];
}
- (UIEdgeInsets)zoomPageEdgesTo:(CGPoint)point {
UIEdgeInsets edges = _pageEdges;
CGFloat zoom = kTapZoom * self.pageWidth;
CGFloat r = self.pageHeight / self.pageWidth;
CGFloat xd = self.pageWidth/2 - point.x;
CGFloat yd = self.pageHeight/2 - point.y;
edges.left = (-zoom + xd);
edges.right = zoom + xd;
edges.top = (-zoom + yd) * r;
edges.bottom = (zoom + yd) * r;
if (edges.left > 0) {
edges.right += edges.left;
edges.left = 0;
} else if (edges.right < 0) {
edges.left += -edges.right;
edges.right = 0;
}
if (edges.top > 0) {
edges.bottom += edges.top;
edges.top = 0;
} else if (edges.bottom < 0) {
edges.top += -edges.bottom;
edges.bottom = 0;
}
return edges;
}
- (UIEdgeInsets)reversePageEdges {
UIEdgeInsets edges = _pageEdges;
edges.left = -edges.left;
edges.right = -edges.right;
edges.top = -edges.top;
edges.bottom = -edges.bottom;
return edges;
}
- (UIEdgeInsets)constrainEdges:(UIEdgeInsets)edges toWidth:(CGFloat)constrainedWidth {
CGFloat constrainedHeight = constrainedWidth * (self.pageHeight/self.pageWidth);
CGFloat height = -edges.top + self.pageHeight + edges.bottom;
CGFloat width = -edges.left + self.pageWidth + edges.right;
CGFloat xd = constrainedWidth - width;
CGFloat yd = constrainedHeight - height;
return UIEdgeInsetsMake(edges.top - yd/2, edges.left - xd/2,
edges.bottom + yd/2, edges.right + xd/2);
}
- (CGFloat)resist:(CGFloat)x1 to:(CGFloat)x2 max:(CGFloat)max {
// The closer we get to the maximum, the less we are allowed to increment
CGFloat rl = (1 - (fabs(x2) / max)) * 0.1;
if (rl < 0) rl = 0;
if (rl > 1) rl = 1;
return x1 + ((x2 - x1) * rl);
}
- (UIEdgeInsets)resistPageEdges:(UIEdgeInsets)edges {
CGFloat left = edges.left, right = edges.right, top = edges.top, bottom = edges.bottom;
CGFloat width = self.pageWidth, height = self.pageHeight;
if (-left + right < 0 || -top + bottom < 0) {
CGFloat zoom = self.zoomFactor;
left = [self resist:_pageEdges.left to:left max:width * zoom];
right = [self resist:_pageEdges.right to:right max:width * zoom];
top = [self resist:_pageEdges.top to:top max:height * zoom];
bottom = [self resist:_pageEdges.bottom to:bottom max:height * zoom];
} else {
if (_touchCount == 2 || self.zoomed) {
if (top > 0) {
top = [self resist:_pageEdges.top to:top max:height];
if (_touchCount == 2) {
bottom = bottom + (top - _pageEdges.top);
} else {
bottom = _pageEdges.bottom + (top - _pageEdges.top);
}
CGFloat newHeight = -top + height + bottom;
CGFloat newWidth = (width/height) * newHeight;
CGFloat xd = newWidth - (-left + width + right);
left -= xd/2;
right += xd/2;
} else if (bottom < 0) {
bottom = [self resist:_pageEdges.bottom to:bottom max:height];
if (_touchCount == 2) {
top = top + (bottom - _pageEdges.bottom);
} else {
top = _pageEdges.top + (bottom - _pageEdges.bottom);
}
CGFloat newHeight = -top + height + bottom;
CGFloat newWidth = (width/height) * newHeight;
CGFloat xd = newWidth - (-left + width + right);
left -= xd/2;
right += xd/2;
}
}
BOOL flipped = self.flipped;
BOOL flickPrevious = (left > 0 && !flipped) || (left < 0 && flipped);
BOOL flickNext = (right < 0 && !flipped) || (right > 0 && flipped);
if (flickPrevious && ([self isFirstPage] || self.zoomed)) {
left = [self resist:_pageEdges.left to:left max:width];
if (_touchCount == 2) {
right = right + (left - _pageEdges.left);
} else {
right = _pageEdges.right + (left - _pageEdges.left);
}
CGFloat newWidth = -left + width + right;
CGFloat newHeight = (height/width) * newWidth;
CGFloat yd = newHeight - (-top + height + bottom);
top -= yd/2;
bottom += yd/2;
} else if (flickNext && ([self isLastPage] || self.zoomed)) {
right = [self resist:_pageEdges.right to:right max:width];
if (_touchCount == 2) {
left = left + (right - _pageEdges.right);
} else {
left = _pageEdges.left + (right - _pageEdges.right);
}
CGFloat newWidth = -left + width + right;
CGFloat newHeight = (height/width) * newWidth;
CGFloat yd = newHeight - (-top + height + bottom);
top -= yd/2;
bottom += yd/2;
}
}
return UIEdgeInsetsMake(top, left, bottom, right);
}
- (UIEdgeInsets)pageEdgesForAnimation {
CGFloat left = 0, right = 0, top = 0, bottom = 0;
if (self.pinched) {
left = -_pageEdges.left;
right = -_pageEdges.right;
top = -_pageEdges.top;
bottom = -_pageEdges.bottom;
} else if (self.flicked) {
CGRect centerFrame = [self frameOfPageAtIndex:_centerPageIndex];
CGFloat centerPageOverflow = [self overflowForFrame:centerFrame] * self.zoomFactor;
if (_pageEdges.left < 0) {
CGRect frame = [self frameOfPageAtIndex:_centerPageIndex + (self.flipped ? -1 : 1)];
CGFloat overflow = centerPageOverflow + [self overflowForFrame:frame];
if (fabs(_pageStartEdges.left) >= fabs(_pageEdges.right)) {
left = right = -((self.pageWidth + _pageSpacing) + _pageEdges.right + _overshoot + overflow);
} else {
left = right = -((self.pageWidth + _pageSpacing) + _pageEdges.left + _overshoot + overflow);
}
} else {
CGRect frame = [self frameOfPageAtIndex:_centerPageIndex + (self.flipped ? 1 : -1)];
CGFloat overflow = centerPageOverflow + [self overflowForFrame:frame];
if (fabs(_pageEdges.left) >= fabs(_pageEdges.right)) {
left = right = ((self.pageWidth + _pageSpacing) - _pageEdges.right + _overshoot + overflow);
} else {
left = right = ((self.pageWidth + _pageSpacing) - _pageEdges.left + _overshoot + overflow);
}
}
} else {
if (_pageEdges.left > 0) {
left = right = -_pageEdges.left;
} else if (_pageEdges.right < 0) {
left = right = -_pageEdges.right;
}
if (_pageEdges.top > 0) {
top = bottom = -_pageEdges.top;
} else if (_pageEdges.bottom < 0) {
top = bottom = -_pageEdges.bottom;
}
}
return UIEdgeInsetsMake(top, left, bottom, right);
}
- (void)acquireTouch:(UITouch*)touch {
if (!_touch1) {
_touch1 = touch;
++_touchCount;
} else if (!_touch2) {
_touch2 = touch;
++_touchCount;
}
}
- (UITouch*)removeTouch:(UITouch*)touch {
if (touch == _touch1) {
_touch1 = nil;
--_touchCount;
return _touch2;
} else if (touch == _touch2) {
_touch2 = nil;
--_touchCount;
return _touch1;
} else {
return nil;
}
}
- (BOOL)canZoom {
return _zoomEnabled
&& (_zooming || ![_delegate respondsToSelector:@selector(scrollViewShouldZoom:)]
|| [_delegate scrollViewShouldZoom:self]);
}
- (BOOL)edgesAreZoomed:(UIEdgeInsets)edges {
return edges.left != edges.right || edges.top != edges.bottom;
}
- (void)updateZooming:(UIEdgeInsets)edges {
if (!_zooming && (self.zoomed || [self edgesAreZoomed:edges])) {
_zooming = YES;
self.centerPage.userInteractionEnabled = NO;
if ([_delegate respondsToSelector:@selector(scrollViewDidBeginZooming:)]) {
[_delegate scrollViewDidBeginZooming:self];
}
} else if (_zooming && !self.zoomed) {
_zooming = NO;
self.centerPage.userInteractionEnabled = YES;
if ([_delegate respondsToSelector:@selector(scrollViewDidEndZooming:)]) {
[_delegate scrollViewDidEndZooming:self];
}
}
}
- (void)stopDragging:(BOOL)willDecelerate {
if (_dragging) {
_dragging = NO;
if ([_delegate respondsToSelector:@selector(scrollViewDidEndDragging:willDecelerate:)]) {
[_delegate scrollViewDidEndDragging:self willDecelerate:willDecelerate];
}
}
}
- (void)rotationDidStop {
if ([_delegate respondsToSelector:@selector(scrollViewDidRotate:)]) {
[_delegate scrollViewDidRotate:self];
}
}
- (void)startTapTimer:(UITouch*)touch {
_tapTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(tapTimer:)
userInfo:touch repeats:NO];
}
- (void)cancelTapTimer {
[_tapTimer invalidate];
_tapTimer = nil;
}
- (void)tapTimer:(NSTimer*)timer {
_tapTimer = nil;
if ([_delegate respondsToSelector:@selector(scrollView:tapped:)]) {
UITouch* touch = timer.userInfo;
[_delegate scrollView:self tapped:touch];
}
}
- (void)startAnimationTo:(UIEdgeInsets)edges duration:(NSTimeInterval)duration {
if (!_animationTimer) {
_pageStartEdges = _pageEdges;
[self updateZooming:edges];
[self cancelTapTimer];
_animateEdges = edges;
_animationDuration = duration;
_animationStartTime = [[NSDate date] retain];
_animationTimer = [NSTimer scheduledTimerWithTimeInterval:0.005 target:self
selector:@selector(animator) userInfo:nil repeats:YES];
}
}
- (void)stopAnimation:(BOOL)resetEdges {
if (_animationTimer) {
[_animationTimer invalidate];
_animationTimer = nil;
[_animationStartTime release];
_animationStartTime = nil;
_overshoot = 0;
[self updateZooming:UIEdgeInsetsZero];
NSInteger realIndex = [self realPageIndex];
if (realIndex != _centerPageIndex || self.pinched) {
[self moveToPageAtIndex:realIndex resetEdges:resetEdges];
}
}
}
- (CGFloat)tween:(NSTimeInterval)t b:(NSTimeInterval)b c:(NSTimeInterval)c d:(NSTimeInterval)d {
return c*((t=t/d-1)*t*t + 1) + b;
}
- (void)animator {
NSTimeInterval kt = -[_animationStartTime timeIntervalSinceNow];
CGFloat pct = kt ? [self tween:kt b:0 c:kt d:_animationDuration]/kt : 0;
if (pct > 1.0) {
pct = 1.0;
}
_pageEdges.left = _pageStartEdges.left + _animateEdges.left * pct;
_pageEdges.right = _pageStartEdges.right + _animateEdges.right * pct;
_pageEdges.top = _pageStartEdges.top + _animateEdges.top * pct;
_pageEdges.bottom = _pageStartEdges.bottom + _animateEdges.bottom * pct;
[self setNeedsLayout];
if (pct == 1.0) {
if (_overshoot) {
[_animationStartTime release];
[_animationTimer invalidate];
_animationTimer = nil;
[self startAnimationTo:UIEdgeInsetsMake(0, self.overshoot, 0, self.overshoot) duration:0.1];
_overshoot = 0;
} else {
[self stopAnimation:NO];
if ([_delegate respondsToSelector:@selector(scrollViewDidEndDecelerating:)]) {
[_delegate scrollViewDidEndDecelerating:self];
}
}
}
}
- (void)animator2 {
NSTimeInterval kt = -[_animationStartTime timeIntervalSinceNow];
CGFloat pct = kt ? [self tween:kt b:0 c:kt d:_animationDuration]/kt : 0;
if (pct > 1.0) {
pct = 1.0;
}
_pageEdges.left = _pageStartEdges.left + _animateEdges.left * pct;
_pageEdges.right = _pageStartEdges.right + _animateEdges.right * pct;
_pageEdges.top = _pageStartEdges.top + _animateEdges.top * pct;
_pageEdges.bottom = _pageStartEdges.bottom + _animateEdges.bottom * pct;
[self setNeedsLayout];
if (pct == 1.0) {
[self stopAnimation:YES];
if ([_delegate respondsToSelector:@selector(scrollViewDidEndDecelerating:)]) {
[_delegate scrollViewDidEndDecelerating:self];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// UIView
- (void)layoutSubviews {
[self layoutPage];
[self layoutAdjacentPages];
if (_visiblePageIndex != _centerPageIndex && self.centerPage) {
_visiblePageIndex = _centerPageIndex;
[_delegate scrollView:self didMoveToPageAtIndex:_centerPageIndex];
}
}
- (void)didMoveToSuperview {
if (!self.superview) {
[self stopAnimation:YES];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
if (_touchCount < 2) {
[self stopAnimation:NO];
for (UITouch* touch in touches) {
[self acquireTouch:touch];
if (_touchCount == 1) {
if ([_delegate respondsToSelector:@selector(scrollView:touchedDown:)]) {
[_delegate scrollView:self touchedDown:touch];
}
if (_scrollEnabled) {
CGPoint pt = [self touchLocation:touch];
_touchStartEdges = _touchEdges = [self touchEdgesForPoint:pt];
_pageStartEdges = _pageEdges;
}
} else if (_touchCount == 2) {
if (_scrollEnabled) {
CGPoint pt = [self touchLocation:touch];
_touchEdges = [self squareTouchEdges:[self stretchTouchEdges:_touchEdges toPoint:pt]];
_touchStartEdges = _touchEdges;
_pageStartEdges = _pageEdges;
}
}
if (touch.tapCount == 2) {
[self cancelTapTimer];
}
}
}
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
if (_scrollEnabled && _touchCount && !_animationTimer) {
if (!_dragging) {
_dragging = YES;
[self cancelTapTimer];
if ([_delegate respondsToSelector:@selector(scrollViewWillBeginDragging:)]) {
[_delegate scrollViewWillBeginDragging:self];
}
}
_touchEdges = UIEdgeInsetsZero;
for (UITouch* touch in [event allTouches]) {
if (touch == _touch1 || touch == _touch2) {
_touchEdges = [self stretchTouchEdges:_touchEdges toPoint:[self touchLocation:touch]];
}
}
UIEdgeInsets edges = [self squareTouchEdges:_touchEdges];
CGFloat left = _pageStartEdges.left + (edges.left - _touchStartEdges.left);
CGFloat right = _pageStartEdges.right + (edges.right - _touchStartEdges.right);
CGFloat top = _pageEdges.top;
CGFloat bottom = _pageEdges.bottom;
if ((_touchCount == 2 || self.zoomed) && _zoomEnabled) {
CGFloat r = self.pageHeight / self.pageWidth;
top = _pageStartEdges.top + (edges.top - _touchStartEdges.top) * r;
bottom = _pageStartEdges.bottom + (edges.bottom - _touchStartEdges.bottom) * r;
}
UIEdgeInsets newEdges = UIEdgeInsetsMake(top, left, bottom, right);
UIEdgeInsets pageEdges = [self resistPageEdges:newEdges];
if (![self edgesAreZoomed:pageEdges] || self.canZoom) {
_pageEdges = pageEdges;
[self updateZooming:pageEdges];
[self setNeedsLayout];
}
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
for (UITouch* touch in touches) {
[self removeTouch:touch];
}
if (!_touchCount) {
[self stopAnimation:YES];
[self stopDragging:NO];
[self updateZooming:UIEdgeInsetsZero];
_pageEdges = UIEdgeInsetsZero;
[self setNeedsLayout];
}
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
[super touchesEnded:touches withEvent:event];
for (UITouch* touch in touches) {
if (touch == _touch1 || touch == _touch2) {
UITouch* remainingTouch = [self removeTouch:touch];
if (_touchCount == 1) {
CGPoint point = [self touchLocation:remainingTouch];
_touchEdges = _touchStartEdges = [self touchEdgesForPoint:point];
_pageStartEdges = _pageEdges;
} else if (_touchCount == 0) {
if (touch.tapCount == 1 && !_dragging) {
if ([_delegate respondsToSelector:@selector(scrollView:touchedUpInside:)]) {
[_delegate scrollView:self touchedUpInside:touch];
}
[self startTapTimer:touch];
} else if (touch.tapCount == 2 && self.canZoom) {
CGPoint pt = [self touchLocation:touch];
if (self.zoomed) {
[self startAnimationTo:[self reversePageEdges] duration:kFlickDuration];
} else {