forked from modocache/MDCParallaxView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMDCParallaxView.m
237 lines (192 loc) · 8.54 KB
/
MDCParallaxView.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
// MDCParallaxView.m
//
// Copyright (c) 2012 modocache
//
// 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 "MDCParallaxView.h"
static CGFloat const kMDCParallaxViewDefaultHeight = 150.0f;
@interface MDCParallaxView () <UIScrollViewDelegate>
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIView *foregroundView;
@property (nonatomic, strong) UIScrollView *backgroundScrollView;
@property (nonatomic, strong) UIScrollView *foregroundScrollView;
- (void)updateBackgroundFrame;
- (void)updateForegroundFrame;
- (void)updateContentOffset;
@end
@implementation MDCParallaxView
#pragma mark - Object Lifecycle
- (id)initWithBackgroundView:(UIView *)backgroundView foregroundView:(UIView *)foregroundView {
self = [super init];
if (self) {
_backgroundHeight = kMDCParallaxViewDefaultHeight;
_backgroundView = backgroundView;
_foregroundView = foregroundView;
_backgroundScrollView = [UIScrollView new];
_backgroundScrollView.backgroundColor = [UIColor clearColor];
_backgroundScrollView.showsHorizontalScrollIndicator = NO;
_backgroundScrollView.showsVerticalScrollIndicator = NO;
[_backgroundScrollView addSubview:_backgroundView];
[self addSubview:_backgroundScrollView];
_foregroundScrollView = [UIScrollView new];
_foregroundScrollView.backgroundColor = [UIColor clearColor];
_foregroundScrollView.delegate = self;
[_foregroundScrollView addSubview:_foregroundView];
[self addSubview:_foregroundScrollView];
}
return self;
}
#pragma mark - UIView Overrides
- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
[self updateBackgroundFrame];
[self updateForegroundFrame];
[self updateContentOffset];
}
- (void)setAutoresizingMask:(UIViewAutoresizing)autoresizingMask {
[super setAutoresizingMask:autoresizingMask];
self.backgroundView.autoresizingMask = autoresizingMask;
self.backgroundScrollView.autoresizingMask = autoresizingMask;
self.foregroundView.autoresizingMask = autoresizingMask;
self.foregroundScrollView.autoresizingMask = autoresizingMask;
}
#pragma mark - UIScrollViewDelegate Protocol Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self updateContentOffset];
if ([self.scrollViewDelegate respondsToSelector:_cmd]) {
[self.scrollViewDelegate scrollViewDidScroll:scrollView];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:_cmd]) {
[self.scrollViewDelegate scrollViewDidEndDecelerating:scrollView];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if ([self.scrollViewDelegate respondsToSelector:_cmd]) {
[self.scrollViewDelegate scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:_cmd]) {
[self.scrollViewDelegate scrollViewDidEndScrollingAnimation:scrollView];
}
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView
withView:(UIView *)view
atScale:(float)scale {
if ([self.scrollViewDelegate respondsToSelector:_cmd]) {
[self.scrollViewDelegate scrollViewDidEndZooming:scrollView withView:view atScale:scale];
}
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:_cmd]) {
[self.scrollViewDelegate scrollViewDidScrollToTop:scrollView];
}
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:_cmd]) {
[self.scrollViewDelegate scrollViewDidZoom:scrollView];
}
}
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:_cmd]) {
return [self.scrollViewDelegate scrollViewShouldScrollToTop:scrollView];
}
return YES;
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:_cmd]) {
[self.scrollViewDelegate scrollViewWillBeginDecelerating:scrollView];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:_cmd]) {
[self.scrollViewDelegate scrollViewWillBeginDragging:scrollView];
}
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
if ([self.scrollViewDelegate respondsToSelector:_cmd]) {
[self.scrollViewDelegate scrollViewWillBeginZooming:scrollView withView:view];
}
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset {
if ([self.scrollViewDelegate respondsToSelector:_cmd]) {
[self.scrollViewDelegate scrollViewWillEndDragging:scrollView
withVelocity:velocity
targetContentOffset:targetContentOffset];
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:_cmd]) {
return [self.scrollViewDelegate viewForZoomingInScrollView:scrollView];
}
return nil;
}
#pragma mark - Public Interface
- (UIScrollView *)scrollView {
return self.foregroundScrollView;
}
- (void)setBackgroundHeight:(CGFloat)backgroundHeight {
_backgroundHeight = backgroundHeight;
[self updateBackgroundFrame];
[self updateForegroundFrame];
[self updateContentOffset];
}
#pragma mark - Internal Methods
- (void)updateBackgroundFrame {
self.backgroundScrollView.frame = CGRectMake(0.0f,
0.0f,
self.frame.size.width,
self.frame.size.height);
self.backgroundScrollView.contentSize = CGSizeMake(self.frame.size.width,
self.frame.size.height);
self.backgroundScrollView.contentOffset = CGPointZero;
self.backgroundView.frame =
CGRectMake(0.0f,
floorf((self.backgroundHeight - self.backgroundView.frame.size.height)/2),
self.frame.size.width,
self.backgroundView.frame.size.height);
}
- (void)updateForegroundFrame {
self.foregroundView.frame = CGRectMake(0.0f,
self.backgroundHeight,
self.foregroundView.frame.size.width,
self.foregroundView.frame.size.height);
self.foregroundScrollView.frame = self.bounds;
self.foregroundScrollView.contentSize =
CGSizeMake(self.foregroundView.frame.size.width,
self.foregroundView.frame.size.height + self.backgroundHeight);
}
- (void)updateContentOffset {
CGFloat offsetY = self.foregroundScrollView.contentOffset.y;
CGFloat threshold = self.backgroundView.frame.size.height - self.backgroundHeight;
if (offsetY > -threshold && offsetY < 0.0f) {
self.backgroundScrollView.contentOffset = CGPointMake(0.0f, floorf(offsetY/2));
} else if (offsetY < 0.0f) {
self.backgroundScrollView.contentOffset = CGPointMake(0.0f, offsetY + floorf(threshold/2));
} else {
self.backgroundScrollView.contentOffset = CGPointMake(0.0f, offsetY);
}
}
@end