forked from ole/CustomScrollView
/
CustomScrollView.m
132 lines (110 loc) · 4.17 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
//
// 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"
@interface CustomScrollView ()
@property CGRect startBounds;
@property (nonatomic, strong) UIDynamicAnimator *animator;
@property (nonatomic, weak) UIDynamicItemBehavior *decelerationBehavior;
@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;
bounds.origin.x = fmax(minBoundsOriginX, fmin(newBoundsOriginX, maxBoundsOriginX));
CGFloat newBoundsOriginY = bounds.origin.y - translation.y;
CGFloat minBoundsOriginY = 0.0;
CGFloat maxBoundsOriginY = self.contentSize.height - bounds.size.height;
bounds.origin.y = fmax(minBoundsOriginY, fmin(newBoundsOriginY, maxBoundsOriginY));
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;
}
}
- (BOOL)scrollVertical
{
return self.contentSize.height > CGRectGetHeight(self.bounds);
}
- (BOOL)scrollHorizontal
{
return self.contentSize.width > CGRectGetWidth(self.bounds);
}
@end