This repository was archived by the owner on Jun 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPullToRefreshView.m
executable file
·230 lines (190 loc) · 8.47 KB
/
PullToRefreshView.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
//
// PullToRefreshView.m
// Grant Paul (chpwn)
//
// (based on EGORefreshTableHeaderView)
//
// Created by Devin Doty on 10/14/09October14.
// Copyright 2009 enormego. All rights reserved.
//
//
// The MIT License (MIT)
// Copyright © 2012 Sonny Parlin, http://sonnyparlin.com
//
// // 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 "PullToRefreshView.h"
#define TEXT_COLOR [UIColor colorWithRed:(87.0/255.0) green:(108.0/255.0) blue:(137.0/255.0) alpha:1.0]
#define FLIP_ANIMATION_DURATION 0.18f
@interface PullToRefreshView (Private)
@property (nonatomic, assign) PullToRefreshViewState state;
@end
@implementation PullToRefreshView
@synthesize delegate, scrollView;
- (void)showActivity:(BOOL)shouldShow animated:(BOOL)animated {
if (shouldShow) [activityView startAnimating];
else [activityView stopAnimating];
[UIView animateWithDuration:(animated ? 0.1f : 0.0) animations:^{
arrowImage.opacity = (shouldShow ? 0.0 : 1.0);
}];
}
- (void)setImageFlipped:(BOOL)flipped {
[UIView animateWithDuration:0.1f animations:^{
arrowImage.transform = (flipped ? CATransform3DMakeRotation(M_PI * 2, 0.0f, 0.0f, 1.0f) : CATransform3DMakeRotation(M_PI, 0.0f, 0.0f, 1.0f));
}];
}
- (id)initWithScrollView:(UIScrollView *)scroll {
CGRect frame = CGRectMake(0.0f, 0.0f - scroll.bounds.size.height, scroll.bounds.size.width, scroll.bounds.size.height);
if ((self = [super initWithFrame:frame])) {
scrollView = scroll;
[scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:NULL];
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.backgroundColor = [UIColor colorWithRed:226.0/255.0 green:231.0/255.0 blue:237.0/255.0 alpha:1.0];
lastUpdatedLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 30.0f, self.frame.size.width, 20.0f)];
lastUpdatedLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
lastUpdatedLabel.font = [UIFont systemFontOfSize:12.0f];
lastUpdatedLabel.textColor = TEXT_COLOR;
lastUpdatedLabel.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
lastUpdatedLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
lastUpdatedLabel.backgroundColor = [UIColor clearColor];
// Line below modified by Eugene Scherba: UITextAlignmentCenter deprecated in iOS6
lastUpdatedLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:lastUpdatedLabel];
statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 48.0f, self.frame.size.width, 20.0f)];
statusLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
statusLabel.font = [UIFont boldSystemFontOfSize:13.0f];
statusLabel.textColor = TEXT_COLOR;
statusLabel.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
statusLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
statusLabel.backgroundColor = [UIColor clearColor];
// Line below modified by Eugene Scherba: UITextAlignmentCenter deprecated in iOS6
statusLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:statusLabel];
arrowImage = [[CALayer alloc] init];
arrowImage.frame = CGRectMake(10.0f, frame.size.height - 60.0f, 24.0f, 52.0f);
arrowImage.contentsGravity = kCAGravityResizeAspect;
arrowImage.contents = (id) [UIImage imageNamed:@"arrow"].CGImage;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
arrowImage.contentsScale = [[UIScreen mainScreen] scale];
}
#endif
[self.layer addSublayer:arrowImage];
activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.frame = CGRectMake(10.0f, frame.size.height - 38.0f, 20.0f, 20.0f);
[self addSubview:activityView];
self.enabled = YES;
[self setState:PullToRefreshViewStateNormal];
}
return self;
}
#pragma mark -
#pragma mark Setters
- (void)setEnabled:(BOOL)enabled
{
if (enabled == _enabled)
return;
_enabled = enabled;
[UIView animateWithDuration:0.25
animations:
^{
self.alpha = enabled ? 1 : 0;
}];
}
- (void)refreshLastUpdatedDate {
NSDate *date = [NSDate date];
if ([delegate respondsToSelector:@selector(pullToRefreshViewLastUpdated:)])
date = [delegate pullToRefreshViewLastUpdated:self];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
lastUpdatedLabel.text = [NSString stringWithFormat:@"Last Updated: %@", [formatter stringFromDate:date]];
[formatter release];
}
- (void)setState:(PullToRefreshViewState)state_ {
state = state_;
switch (state) {
case PullToRefreshViewStateReady:
statusLabel.text = @"Release to update...";
[self showActivity:NO animated:NO];
[self setImageFlipped:YES];
scrollView.contentInset = UIEdgeInsetsZero;
break;
case PullToRefreshViewStateNormal:
statusLabel.text = @"Pull down to update...";
[self showActivity:NO animated:NO];
[self setImageFlipped:NO];
[self refreshLastUpdatedDate];
scrollView.contentInset = UIEdgeInsetsZero;
break;
case PullToRefreshViewStateLoading:
statusLabel.text = @"Loading...";
[self showActivity:YES animated:YES];
[self setImageFlipped:NO];
scrollView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);
break;
default:
break;
}
}
#pragma mark -
#pragma mark UIScrollView
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"contentOffset"] && self.isEnabled) {
if (scrollView.isDragging) {
if (state == PullToRefreshViewStateReady) {
if (scrollView.contentOffset.y > -65.0f && scrollView.contentOffset.y < 0.0f)
[self setState:PullToRefreshViewStateNormal];
} else if (state == PullToRefreshViewStateNormal) {
if (scrollView.contentOffset.y < -65.0f)
[self setState:PullToRefreshViewStateReady];
} else if (state == PullToRefreshViewStateLoading) {
if (scrollView.contentOffset.y >= 0)
scrollView.contentInset = UIEdgeInsetsZero;
else
scrollView.contentInset = UIEdgeInsetsMake(MIN(-scrollView.contentOffset.y, 60.0f), 0, 0, 0);
}
} else {
if (state == PullToRefreshViewStateReady) {
[UIView animateWithDuration:0.2f animations:^{
[self setState:PullToRefreshViewStateLoading];
}];
if ([delegate respondsToSelector:@selector(pullToRefreshViewShouldRefresh:)])
[delegate pullToRefreshViewShouldRefresh:self];
}
}
self.frame = CGRectMake(scrollView.contentOffset.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
}
}
- (void)finishedLoading {
if (state == PullToRefreshViewStateLoading) {
[UIView animateWithDuration:0.3f animations:^{
[self setState:PullToRefreshViewStateNormal];
}];
}
}
#pragma mark -
#pragma mark Dealloc
- (void)dealloc {
[scrollView removeObserver:self forKeyPath:@"contentOffset"];
scrollView = nil;
[super dealloc];
}
@end