-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathECStretchableHeaderView.m
More file actions
381 lines (315 loc) · 13.5 KB
/
Copy pathECStretchableHeaderView.m
File metadata and controls
381 lines (315 loc) · 13.5 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
//
// ECStretchableHeaderView.m
// StretchableHeaderViewExample
//
// Created by Eric Castro on 30/07/14.
// Copyright (c) 2014 cast.ro. All rights reserved.
//
#import <pop/POP.h>
#import <HTDelegateProxy/HTDelegateProxy.h>
#import "ECStretchableHeaderView.h"
@implementation ECStretchableHeaderView
{
UIPanGestureRecognizer *_panGestureRecognizer;
UITapGestureRecognizer *_tapGestureRecognizer;
CGPoint _lastPanLocation;
BOOL _touchesStartedOnSelf;
CGFloat _inset;
HTDelegateProxy *_delegateProxy;
id _originalScrollViewDelegate;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self _setupView];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self _setupView];
}
return self;
}
- (void)_setupView
{
_panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didPan:)];
[self addGestureRecognizer:_panGestureRecognizer];
_panGestureRecognizer.delegate = self;
_tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap:)];
[self addGestureRecognizer:_tapGestureRecognizer];
_tapGestureRecognizer.delegate = self;
self.clipsToBounds = YES;
self.translatesAutoresizingMaskIntoConstraints = NO;
_minHeight = 0.0;
_maxHeight = CGRectGetHeight(self.frame);
_touchesStartedOnSelf = NO;
_tapToExpand = NO;
_compensateBottomScrollingArea = NO;
_resizingEnabled = YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return NO;
}
- (void)didTap:(id)sender
{
CGPoint newOffset = self.attachedScrollView.contentOffset;
newOffset.y -= self.maxHeight - CGRectGetHeight(self.frame);
[UIView animateWithDuration:0.2f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.attachedScrollView setContentOffset:newOffset animated:YES];
} completion:nil];
}
- (void)didPan:(id)sender
{
CGPoint currentLocation = [_panGestureRecognizer locationInView:self];
if (_panGestureRecognizer.state == UIGestureRecognizerStateBegan)
{
_lastPanLocation = currentLocation;
[self.attachedScrollView pop_removeAllAnimations];
[self.attachedScrollView setContentOffset:self.attachedScrollView.contentOffset animated:NO];
}
if (_panGestureRecognizer.state == UIGestureRecognizerStateChanged)
{
CGFloat heightDiff = currentLocation.y - _lastPanLocation.y;
if (self.attachedScrollView.contentOffset.y <= -self.attachedScrollView.contentInset.top)
{
heightDiff = heightDiff/3;
}
self.attachedScrollView.contentOffset = CGPointMake(self.attachedScrollView.contentOffset.x, self.attachedScrollView.contentOffset.y - heightDiff);
}
if (
_panGestureRecognizer.state == UIGestureRecognizerStateEnded ||
_panGestureRecognizer.state == UIGestureRecognizerStateCancelled||
_panGestureRecognizer.state == UIGestureRecognizerStateFailed)
{
if (self.attachedScrollView.contentOffset.y <= -self.attachedScrollView.contentInset.top)
{
_touchesStartedOnSelf = YES;
[self _performBounceBackToTopAnimation];
}
else
{
_touchesStartedOnSelf = YES;
CGPoint velocity = [_panGestureRecognizer velocityInView:self];
if (self.attachedScrollView.bounds.size.width >= self.attachedScrollView.contentSize.width) velocity.x = 0;
//if (self.attachedScrollView.bounds.size.height >= self.attachedScrollView.contentSize.height) velocity.y = 0;
velocity.x = -velocity.x;
velocity.y = -velocity.y;
[self _performDecelerateAnimationWithVelocity:velocity completionBlock:^{
_touchesStartedOnSelf = NO;
}];
}
}
_lastPanLocation = currentLocation;
}
- (void)_performDecelerateAnimationWithVelocity:(CGPoint)velocity completionBlock:(void (^)())completionBlock
{
POPDecayAnimation *decayAnimation = [POPDecayAnimation animation];
POPAnimatableProperty *prop = [POPAnimatableProperty propertyWithName:@"com.rounak.boundsY" initializer:^(POPMutableAnimatableProperty *prop) {
// read value, feed data to Pop
prop.readBlock = ^(id obj, CGFloat values[]) {
values[0] = [obj bounds].origin.x;
values[1] = [obj bounds].origin.y;
};
// write value, get data from Pop, and apply it to the view
prop.writeBlock = ^(id obj, const CGFloat values[]) {
CGRect tempBounds = [obj bounds];
tempBounds.origin.x = values[0];
CGFloat topBoundCheck = values[1] + CGRectGetHeight(self.frame) + _inset;
CGFloat velocityThreshold = ((NSValue *)decayAnimation.velocity).CGPointValue.y / 10.0f * 0.2f;
if (velocityThreshold < 0 && topBoundCheck < velocityThreshold && values[1] < self.maxHeight)
{
[self.attachedScrollView pop_removeAllAnimations];
_touchesStartedOnSelf = YES;
tempBounds.origin.y = - CGRectGetHeight(self.frame) - _inset + topBoundCheck;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self _performBounceBackToTopAnimation];
});
}
else if (tempBounds.origin.y > _attachedScrollView.contentSize.height - _attachedScrollView.frame.size.height + _attachedScrollView.contentInset.bottom + velocityThreshold)
{
[self.attachedScrollView pop_removeAllAnimations];
_touchesStartedOnSelf = YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self _performBounceBackToBottomAnimation];
});
}
else
tempBounds.origin.y = values[1];
[self _scrollView:_attachedScrollView offsetChanged:@{@"new":[NSValue valueWithCGSize:CGSizeMake(0.0f, values[1])], @"old":[NSValue valueWithCGSize:CGSizeMake(0.0f, values[1])]}];
[obj setBounds:tempBounds];
};
// dynamics threshold
prop.threshold = 0.01;
}];
decayAnimation.property = prop;
decayAnimation.velocity = [NSValue valueWithCGPoint:velocity];
decayAnimation.completionBlock = ^(POPAnimation *anim, BOOL finished) { if (completionBlock) completionBlock(); };
[self.attachedScrollView pop_addAnimation:decayAnimation forKey:@"decelerate"];
}
- (POPBasicAnimation *)_bounceBackAnimation
{
POPBasicAnimation *basicAnimation = [POPBasicAnimation animation];
POPAnimatableProperty *prop = [POPAnimatableProperty propertyWithName:@"com.rounak.boundsY" initializer:^(POPMutableAnimatableProperty *prop) {
prop.readBlock = ^(id obj, CGFloat values[]) {
values[0] = [obj bounds].origin.x;
values[1] = [obj bounds].origin.y;
};
prop.writeBlock = ^(id obj, const CGFloat values[]) {
CGRect tempBounds = [obj bounds];
tempBounds.origin.x = values[0];
tempBounds.origin.y = values[1];
[obj setBounds:tempBounds];
};
// dynamics threshold
prop.threshold = 0.01;
}];
basicAnimation.property = prop;
basicAnimation.duration = 0.3f;
basicAnimation.completionBlock = ^(POPAnimation *anim, BOOL finished) {
_touchesStartedOnSelf = NO;
};
return basicAnimation;
}
- (void)_performBounceBackToTopAnimation
{
POPBasicAnimation *basicAnimation = [self _bounceBackAnimation];
basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.attachedScrollView.contentOffset.x, - CGRectGetHeight(self.frame) - _inset)];
[self.attachedScrollView pop_addAnimation:basicAnimation forKey:@"bounceBack"];
}
- (void)_performBounceBackToBottomAnimation
{
POPBasicAnimation *basicAnimation = [self _bounceBackAnimation];
if (self.attachedScrollView.contentSize.height < self.attachedScrollView.frame.size.height) //fixes weird jump bug
{
basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.attachedScrollView.contentOffset.x, 0.0f)];
}
else
{
basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.attachedScrollView.contentOffset.x,self.attachedScrollView.contentSize.height - self.attachedScrollView.bounds.size.height)];
}
[self.attachedScrollView pop_addAnimation:basicAnimation forKey:@"bounceBack"];
}
- (BOOL)changeHeightBy:(CGFloat)heightDiff
{
CGRect frame = self.frame;
if (frame.size.height + heightDiff > self.maxHeight)
frame.size.height = self.maxHeight;
else if (frame.size.height + heightDiff < self.minHeight)
frame.size.height = self.minHeight;
else
frame.size.height += heightDiff;
if (self.heightConstraint)
{
self.heightConstraint.constant = frame.size.height;
}
else
{
self.frame = frame;
}
return YES;
}
- (void)attachToScrollView:(UIScrollView *)scrollView inset:(CGFloat)inset
{
[self attachToScrollView:scrollView parentView:scrollView.superview inset:inset];
}
- (void)attachToScrollView:(UIScrollView *)scrollView parentView:(UIView *)parentView inset:(CGFloat)inset
{
_inset = inset;
CGRect frame = self.frame;
frame.origin.x = parentView.frame.origin.x;
frame.origin.y = parentView.frame.origin.y + inset;
frame.size.width = CGRectGetWidth(parentView.frame);
self.frame = frame;
[parentView addSubview:self];
self.attachedScrollView = scrollView;
}
- (void)setAttachedScrollView:(UIScrollView *)attachedScrollView
{
if (_attachedScrollView)
{
_attachedScrollView.delegate = _originalScrollViewDelegate==[NSNull null] ? nil : _originalScrollViewDelegate;
[_attachedScrollView removeObserver:self forKeyPath:@"contentOffset"];
}
_attachedScrollView = attachedScrollView;
_originalScrollViewDelegate = _attachedScrollView.delegate ? _attachedScrollView.delegate : [NSNull null];
_delegateProxy = [[HTDelegateProxy alloc] initWithDelegates:@[_originalScrollViewDelegate, self]];
_attachedScrollView.delegate = (id<UIScrollViewDelegate>)_delegateProxy;
UIEdgeInsets contentInset = attachedScrollView.contentInset;
contentInset.top = self.maxHeight;
attachedScrollView.contentInset = contentInset;
attachedScrollView.scrollIndicatorInsets = contentInset;
attachedScrollView.contentOffset = CGPointMake(attachedScrollView.contentOffset.x, -CGRectGetHeight(self.frame));
[attachedScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:NULL];
[attachedScrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:NULL];
[self _scrollView:attachedScrollView sizeChanged:@{@"new":[NSValue valueWithCGSize:attachedScrollView.contentSize]}];
_minOffset = 0.0f;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
UIScrollView *scrollView = object;
if ([keyPath isEqualToString:@"contentOffset"])
{
[self _scrollView:scrollView offsetChanged:change];
}
if ([keyPath isEqualToString:@"contentSize"])
{
[self _scrollView:scrollView sizeChanged:change];
}
}
- (void)_scrollView:(UIScrollView *)scrollView offsetChanged:(NSDictionary *)change
{
if (!self.resizingEnabled) return;
NSValue *oldValue = [change valueForKey:@"old"];
NSValue *newValue = [change valueForKey:@"new"];
CGFloat oldYOffset = oldValue.CGPointValue.y;
CGFloat newYOffset = newValue.CGPointValue.y;
CGFloat offsetDiff = oldYOffset-newYOffset;
CGRect frame = self.frame;
frame.origin.y = newYOffset + _inset;
//self.frame = frame;
if (newYOffset > scrollView.contentSize.height - scrollView.frame.size.height + scrollView.contentInset.bottom)
{
//bouncing at the bottom; do nothing
}
CGFloat relativePosition = newYOffset + self.maxHeight - self.minOffset;
CGFloat heightCheck = self.maxHeight - relativePosition;
if (relativePosition >= 0.0f)
{
if (heightCheck < self.minHeight)
self.heightConstraint.constant = self.minHeight;
else
self.heightConstraint.constant = self.maxHeight - relativePosition;
}
else
self.heightConstraint.constant = self.maxHeight;
}
- (void)_scrollView:(UIScrollView *)scrollView sizeChanged:(NSDictionary *)change
{
if (!self.resizingEnabled) return;
NSValue *newValue = [change valueForKey:@"new"];
UIEdgeInsets contentInset = scrollView.contentInset;
if (scrollView.contentSize.height < scrollView.frame.size.height)
{
contentInset.bottom = (scrollView.frame.size.height - newValue.CGSizeValue.height) ;
}
else
{
contentInset.bottom = (_compensateBottomScrollingArea ? self.maxHeight : 0.0f);
}
scrollView.contentInset = contentInset;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.attachedScrollView pop_removeAllAnimations];
}
- (void)dealloc
{
[self.attachedScrollView removeObserver:self forKeyPath:@"contentSize"];
[self.attachedScrollView removeObserver:self forKeyPath:@"contentOffset"];
}
@end