forked from soffes/SSPullToRefresh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSPullToRefreshView.m
327 lines (253 loc) · 9.17 KB
/
SSPullToRefreshView.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
//
// SSPullToRefreshView.m
// SSPullToRefresh
//
// Created by Sam Soffes on 4/9/12.
// Copyright (c) 2012 Sam Soffes. All rights reserved.
//
#import "SSPullToRefreshView.h"
#import "SSPullToRefreshDefaultContentView.h"
@interface SSPullToRefreshView ()
@property (nonatomic, assign, readwrite) SSPullToRefreshViewState state;
@property (nonatomic, assign, readwrite) UIScrollView *scrollView;
@property (nonatomic, assign, readwrite, getter = isExpanded) BOOL expanded;
- (void)_setContentInsetTop:(CGFloat)topInset;
- (void)_setState:(SSPullToRefreshViewState)state animated:(BOOL)animated expanded:(BOOL)expanded completion:(void (^)(void))completion;
- (void)_setPullProgress:(CGFloat)pullProgress;
@end
@implementation SSPullToRefreshView {
dispatch_semaphore_t _animationSemaphore;
CGFloat _topInset;
}
@synthesize delegate = _delegate;
@synthesize scrollView = _scrollView;
@synthesize expandedHeight = _expandedHeight;
@synthesize contentView = _contentView;
@synthesize state = _state;
@synthesize expanded = _expanded;
@synthesize defaultContentInset = _defaultContentInset;
#pragma mark - Accessors
- (void)setState:(SSPullToRefreshViewState)state {
BOOL loading = _state == SSPullToRefreshViewStateLoading;
_state = state;
// Forward to content view
[self.contentView setState:_state withPullToRefreshView:self];
// Update delegate
if (loading && _state != SSPullToRefreshViewStateLoading) {
if ([_delegate respondsToSelector:@selector(pullToRefreshViewDidFinishLoading:)]) {
[_delegate pullToRefreshViewDidFinishLoading:self];
}
} else if (!loading && _state == SSPullToRefreshViewStateLoading) {
[self _setPullProgress:1.0f];
if ([_delegate respondsToSelector:@selector(pullToRefreshViewDidStartLoading:)]) {
[_delegate pullToRefreshViewDidStartLoading:self];
}
}
}
- (void)setExpanded:(BOOL)expanded {
_expanded = expanded;
[self _setContentInsetTop:expanded ? self.expandedHeight : 0.0f];
}
- (void)setScrollView:(UIScrollView *)scrollView {
void *context = (__bridge void *)self;
if ([_scrollView respondsToSelector:@selector(removeObserver:forKeyPath:context:)]) {
[_scrollView removeObserver:self forKeyPath:@"contentOffset" context:context];
} else if (_scrollView) {
[_scrollView removeObserver:self forKeyPath:@"contentOffset"];
}
_scrollView = scrollView;
_defaultContentInset = _scrollView.contentInset;
[_scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:context];
}
- (UIView<SSPullToRefreshContentView> *)contentView {
// Use the simple content view as the default
if (!_contentView) {
self.contentView = [[SSPullToRefreshDefaultContentView alloc] initWithFrame:CGRectZero];
}
return _contentView;
}
- (void)setContentView:(UIView<SSPullToRefreshContentView> *)contentView {
[_contentView removeFromSuperview];
_contentView = contentView;
_contentView.autoresizingMask = UIViewAutoresizingNone;
[_contentView setState:_state withPullToRefreshView:self];
[self refreshLastUpdatedAt];
[self addSubview:_contentView];
}
- (void)setDefaultContentInset:(UIEdgeInsets)defaultContentInset {
_defaultContentInset = defaultContentInset;
[self _setContentInsetTop:_topInset];
}
#pragma mark - NSObject
- (void)dealloc {
self.scrollView = nil;
self.delegate = nil;
dispatch_release(_animationSemaphore);
}
#pragma mark - UIView
- (void)removeFromSuperview {
self.scrollView = nil;
[super removeFromSuperview];
}
- (void)layoutSubviews {
CGSize size = self.bounds.size;
CGSize contentSize = [self.contentView sizeThatFits:size];
if (contentSize.width < size.width) {
contentSize.width = size.width;
}
if (contentSize.height < _expandedHeight) {
contentSize.height = _expandedHeight;
}
self.contentView.frame = CGRectMake(roundf((size.width - contentSize.width) / 2.0f), size.height - contentSize.height, contentSize.width, contentSize.height);
}
#pragma mark - Initializer
- (id)initWithScrollView:(UIScrollView *)scrollView delegate:(id<SSPullToRefreshViewDelegate>)delegate {
CGRect frame = CGRectMake(0.0f, 0.0f - scrollView.bounds.size.height, scrollView.bounds.size.width,
scrollView.bounds.size.height);
if ((self = [self initWithFrame:frame])) {
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.scrollView = scrollView;
self.delegate = delegate;
self.state = SSPullToRefreshViewStateNormal;
self.expandedHeight = 70.0f;
// Add to scroll view
[self.scrollView addSubview:self];
// Semaphore is used to ensure only one animation plays at a time
_animationSemaphore = dispatch_semaphore_create(0);
dispatch_semaphore_signal(_animationSemaphore);
}
return self;
}
#pragma mark - Loading
- (void)startLoading {
[self startLoadingAndExpand:NO];
}
- (void)startLoadingAndExpand:(BOOL)shouldExpand {
// If we're not loading, this method has no effect
if (_state == SSPullToRefreshViewStateLoading) {
return;
}
// Animate back to the loading state
[self _setState:SSPullToRefreshViewStateLoading animated:YES expanded:shouldExpand completion:nil];
}
- (void)finishLoading {
// If we're not loading, this method has no effect
if (_state != SSPullToRefreshViewStateLoading) {
return;
}
// Animate back to the normal state
[self _setState:SSPullToRefreshViewStateClosing animated:YES expanded:NO completion:^{
self.state = SSPullToRefreshViewStateNormal;
}];
}
- (void)refreshLastUpdatedAt {
NSDate *date = nil;
if ([_delegate respondsToSelector:@selector(pullToRefreshViewLastUpdatedAt:)]) {
date = [_delegate pullToRefreshViewLastUpdatedAt:self];
} else {
date = [NSDate date];
}
// Forward to content view
if ([self.contentView respondsToSelector:@selector(setLastUpdatedAt:withPullToRefreshView:)]) {
[self.contentView setLastUpdatedAt:date withPullToRefreshView:self];
}
}
#pragma mark - Private
- (void)_setContentInsetTop:(CGFloat)topInset {
_topInset = topInset;
// Default to the scroll view's initial content inset
UIEdgeInsets inset = _defaultContentInset;
// Add the top inset
inset.top += _topInset;
// Don't set it if that is already the current inset
if (UIEdgeInsetsEqualToEdgeInsets(_scrollView.contentInset, inset)) {
return;
}
// Update the content inset
_scrollView.contentInset = inset;
}
- (void)_setState:(SSPullToRefreshViewState)state animated:(BOOL)animated expanded:(BOOL)expanded completion:(void (^)(void))completion {
if (!animated) {
self.state = state;
self.expanded = expanded;
if (completion) {
completion();
}
return;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
dispatch_semaphore_wait(_animationSemaphore, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self.state = state;
self.expanded = expanded;
} completion:^(BOOL finished) {
dispatch_semaphore_signal(_animationSemaphore);
if (completion) {
completion();
}
}];
});
});
}
- (void)_setPullProgress:(CGFloat)pullProgress {
if ([self.contentView respondsToSelector:@selector(setPullProgress:)]) {
[self.contentView setPullProgress:pullProgress];
}
}
#pragma mark - NSKeyValueObserving
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
// Call super if we didn't register for this notification
if (context != (__bridge void *)self) {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
return;
}
// We don't care about this notificaiton
if (object != _scrollView || ![keyPath isEqualToString:@"contentOffset"]) {
return;
}
// Get the offset out of the change notification
CGFloat y = [[change objectForKey:NSKeyValueChangeNewKey] CGPointValue].y;
// Scroll view is dragging
if (_scrollView.isDragging) {
// Scroll view is ready
if (_state == SSPullToRefreshViewStateReady) {
// Dragged enough to refresh
if (y > -_expandedHeight && y < 0.0f) {
self.state = SSPullToRefreshViewStateNormal;
}
// Scroll view is normal
} else if (_state == SSPullToRefreshViewStateNormal) {
// Update the content view's pulling progressing
[self _setPullProgress:-y / _expandedHeight];
// Dragged enough to be ready
if (y < -_expandedHeight) {
self.state = SSPullToRefreshViewStateReady;
}
// Scroll view is loading
} else if (_state == SSPullToRefreshViewStateLoading) {
if (y >= 0.0f) {
[self _setContentInsetTop:0.0f];
} else {
[self _setContentInsetTop:MIN(-y, _expandedHeight)];
}
}
return;
}
// If the scroll view isn't ready, we're not interested
if (_state != SSPullToRefreshViewStateReady) {
return;
}
// We're ready, prepare to switch to loading. Be default, we should refresh.
SSPullToRefreshViewState newState = SSPullToRefreshViewStateLoading;
// Ask the delegate if it's cool to start loading
if ([_delegate respondsToSelector:@selector(pullToRefreshViewShouldStartLoading:)]) {
if (![_delegate pullToRefreshViewShouldStartLoading:self]) {
// Animate back to normal since the delegate said no
newState = SSPullToRefreshViewStateNormal;
}
}
// Animate to the new state
[self _setState:newState animated:YES expanded:YES completion:nil];
}
@end