forked from ole/CustomScrollView
/
CustomScrollView.m
179 lines (149 loc) · 6.54 KB
/
CustomScrollView.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
//
// CustomScrollView.m
// CustomScrollView
//
// Created by Ole Begemann on 16.04.14.
// Copyright (c) 2014 Ole Begemann. All rights reserved.
// Parts of the class are based on https://github.com/grp/CustomScrollView/blob/custom-scroll-with-pop/CustomScrollView/CustomScrollView.m
#import "CustomScrollView.h"
#import "CSCDynamicItem.h"
static CGFloat rubberBandDistance(CGFloat offset, CGFloat dimension) {
const CGFloat constant = 0.55f;
CGFloat result = (constant * abs(offset) * dimension) / (dimension + constant * abs(offset));
// The algorithm expects a positive offset, so we have to negate the result if the offset was negative.
return offset < 0.0f ? -result : result;
}
@interface CustomScrollView ()
@property CGRect startBounds;
@property (nonatomic, strong) UIDynamicAnimator *animator;
@property (nonatomic, weak) UIDynamicItemBehavior *decelerationBehavior;
@property (nonatomic, weak) UIAttachmentBehavior *springBehavior;
@property (nonatomic, strong) CSCDynamicItem *dynamicItem;
@end
@implementation CustomScrollView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self == nil) {
return nil;
}
[self commonInitForCustomScrollView];
return self;
}
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self == nil) {
return nil;
}
[self commonInitForCustomScrollView];
return self;
}
- (void)commonInitForCustomScrollView
{
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[self addGestureRecognizer:panGestureRecognizer];
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self];
self.dynamicItem = [[CSCDynamicItem alloc] init];
}
- (void)handlePanGesture:(UIPanGestureRecognizer *)panGestureRecognizer
{
switch (panGestureRecognizer.state) {
case UIGestureRecognizerStateBegan:
{
self.startBounds = self.bounds;
[self.animator removeAllBehaviors];
}
// fall through
case UIGestureRecognizerStateChanged:
{
CGPoint translation = [panGestureRecognizer translationInView:self];
CGRect bounds = self.startBounds;
if (!self.scrollHorizontal) {
translation.x = 0.0;
}
if (!self.scrollVertical) {
translation.y = 0.0;
}
CGFloat newBoundsOriginX = bounds.origin.x - translation.x;
CGFloat minBoundsOriginX = 0.0;
CGFloat maxBoundsOriginX = self.contentSize.width - bounds.size.width;
CGFloat constrainedBoundsOriginX = fmax(minBoundsOriginX, fmin(newBoundsOriginX, maxBoundsOriginX));
CGFloat rubberBandedX = rubberBandDistance(newBoundsOriginX - constrainedBoundsOriginX, CGRectGetWidth(self.bounds));
bounds.origin.x = constrainedBoundsOriginX + rubberBandedX;
CGFloat newBoundsOriginY = bounds.origin.y - translation.y;
CGFloat minBoundsOriginY = 0.0;
CGFloat maxBoundsOriginY = self.contentSize.height - bounds.size.height;
CGFloat constrainedBoundsOriginY = fmax(minBoundsOriginY, fmin(newBoundsOriginY, maxBoundsOriginY));
CGFloat rubberBandedY = rubberBandDistance(newBoundsOriginY - constrainedBoundsOriginY, CGRectGetHeight(self.bounds));
bounds.origin.y = constrainedBoundsOriginY + rubberBandedY;
self.bounds = bounds;
}
break;
case UIGestureRecognizerStateEnded:
{
CGPoint velocity = [panGestureRecognizer velocityInView:self];
if (![self scrollHorizontal]) {
velocity.x = 0;
}
if (![self scrollVertical]) {
velocity.y = 0;
}
velocity.x = -velocity.x;
velocity.y = -velocity.y;
self.dynamicItem.center = self.bounds.origin;
UIDynamicItemBehavior *decelerationBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.dynamicItem]];
[decelerationBehavior addLinearVelocity:velocity forItem:self.dynamicItem];
decelerationBehavior.resistance = 2.0;
__weak typeof(self)weakSelf = self;
decelerationBehavior.action = ^{
// IMPORTANT: If the deceleration behavior is removed, the bounds' origin will stop updating. See other possible ways of updating origin in the accompanying blog post.
CGRect bounds = weakSelf.bounds;
bounds.origin = weakSelf.dynamicItem.center;
weakSelf.bounds = bounds;
};
[self.animator addBehavior:decelerationBehavior];
self.decelerationBehavior = decelerationBehavior;
}
break;
default:
break;
}
}
- (void)setBounds:(CGRect)bounds
{
[super setBounds:bounds];
CGPoint maxBoundsOrigin = CGPointMake(self.contentSize.width - bounds.size.width,
self.contentSize.height - bounds.size.height);
BOOL outsideBoundsMinimum = bounds.origin.x < 0.0 || bounds.origin.y < 0.0;
BOOL outsideBoundsMaximum = bounds.origin.x > maxBoundsOrigin.x || bounds.origin.y > maxBoundsOrigin.y;
if ((outsideBoundsMaximum || outsideBoundsMinimum) &&
(self.decelerationBehavior && !self.springBehavior)) {
CGPoint target = bounds.origin;
if (outsideBoundsMinimum) {
target.x = fmin(maxBoundsOrigin.x, fmax(target.x, 0.0));
target.y = fmin(maxBoundsOrigin.y, fmax(target.y, 0.0));
} else if (outsideBoundsMaximum) {
// `fmax` is used to fix the case when contentSize is smaller than bounds
target.x = fmax(0, fmin(target.x, maxBoundsOrigin.x));
target.y = fmax(0, fmin(target.y, maxBoundsOrigin.y));
}
UIAttachmentBehavior *springBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.dynamicItem attachedToAnchor:target];
// Has to be equal to zero, because otherwise the bounds.origin wouldn't exactly match the target's position.
springBehavior.length = 0;
// These two values were chosen by trial and error.
springBehavior.damping = 1;
springBehavior.frequency = 2;
[self.animator addBehavior:springBehavior];
self.springBehavior = springBehavior;
}
}
- (BOOL)scrollVertical
{
return self.contentSize.height > CGRectGetHeight(self.bounds);
}
- (BOOL)scrollHorizontal
{
return self.contentSize.width > CGRectGetWidth(self.bounds);
}
@end