/
EFRefreshHeaderView.m
executable file
·238 lines (190 loc) · 8.56 KB
/
EFRefreshHeaderView.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
//
// This is basically EGORefreshTableHeaderView, but heavily cleaned up by Johan Kool.
//
//
// EGORefreshTableHeaderView.m
// Demo
//
// Created by Devin Doty on 10/14/09October14.
// Copyright 2009 enormego. All rights reserved.
//
// 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 "EFRefreshHeaderView.h"
#define TEXT_COLOR [UIColor whiteColor]
//[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 EFRefreshHeaderView () {
PullRefreshState state_;
}
@property (nonatomic, retain) CALayer *arrowImage;
@property (nonatomic, retain) UIActivityIndicatorView *activityView;
@property (nonatomic, assign) PullRefreshState state;
- (void)setupWithFrame:(CGRect)frame arrowImageName:(NSString *)arrow textColor:(UIColor *)textColor;
@end
@implementation EFRefreshHeaderView
@synthesize delegate=delegate_;
@synthesize instructionLabel=instructionLabel_;
@synthesize statusLabel=statusLabel_;
@synthesize arrowImage=arrowImage_;
@synthesize activityView=activityView_;
- (id)initWithFrame:(CGRect)frame arrowImageName:(NSString *)arrow textColor:(UIColor *)textColor {
if ((self = [super initWithFrame:frame])) {
[self setupWithFrame:frame arrowImageName:arrow textColor:textColor];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
return [self initWithFrame:frame arrowImageName:@"whiteArrow.png" textColor:TEXT_COLOR];
}
- (void)awakeFromNib {
CGRect frame = self.frame;
UIColor *textColor = TEXT_COLOR;
NSString *arrow = @"blueArrow.png";
[self setupWithFrame:frame arrowImageName:arrow textColor:textColor];
}
- (void)setupWithFrame:(CGRect)frame arrowImageName:(NSString *)arrow textColor:(UIColor *)textColor {
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.backgroundColor = [UIColor orangeColor];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(48.0f, frame.size.height - 48.0f, frame.size.width - 48.0f, 32.0f)] autorelease];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.numberOfLines = 2;
label.font = [UIFont systemFontOfSize:14.0f];
label.textColor = textColor;
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentLeft;
[self addSubview:label];
self.instructionLabel = label;
// Note: statusLabel not used in this app
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(8.0f, frame.size.height - 65.0f, 30.0f, 55.0f);
layer.contentsGravity = kCAGravityResizeAspect;
layer.contents = (id)[UIImage imageNamed : arrow].CGImage;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
layer.contentsScale = [[UIScreen mainScreen] scale];
}
#endif
[[self layer] addSublayer:layer];
self.arrowImage = layer;
UIActivityIndicatorView *view = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
view.frame = CGRectMake(20.0f, frame.size.height - 44.0f, 20.0f, 20.0f);
[self addSubview:view];
self.activityView = view;
self.state = PullRefreshNormal;
}
- (PullRefreshState)state {
return state_;
}
- (void)setState:(PullRefreshState)state {
switch (state) {
case PullRefreshPulling:
self.instructionLabel.text = NSLocalizedString(@"Laat de kolom los om de nieuwste berichten te zien", @"Release to refresh status");
[CATransaction begin];
[CATransaction setAnimationDuration:FLIP_ANIMATION_DURATION];
self.arrowImage.transform = CATransform3DMakeRotation((M_PI / 180.0) * 180.0f, 0.0f, 0.0f, 1.0f);
[CATransaction commit];
break;
case PullRefreshNormal:
if (self.state == PullRefreshPulling) {
[CATransaction begin];
[CATransaction setAnimationDuration:FLIP_ANIMATION_DURATION];
self.arrowImage.transform = CATransform3DIdentity;
[CATransaction commit];
}
self.instructionLabel.text = NSLocalizedString(@"Trek de kolom omlaag om de nieuwste berichten te zien", @"Pull down to refresh status");
[self.activityView stopAnimating];
[CATransaction begin];
[CATransaction setValue:(id) kCFBooleanTrue forKey:kCATransactionDisableActions];
self.arrowImage.hidden = NO;
self.arrowImage.transform = CATransform3DIdentity;
[CATransaction commit];
if ([self.delegate respondsToSelector:@selector(refreshHeaderViewStatusLabelText:)]) {
self.statusLabel.text = [self.delegate refreshHeaderViewStatusLabelText:self];
} else {
self.statusLabel.text = nil;
}
break;
case PullRefreshLoading:
self.instructionLabel.text = NSLocalizedString(@"Nieuwste berichten laden…", @"Loading Status");
[self.activityView startAnimating];
[CATransaction begin];
[CATransaction setValue:(id) kCFBooleanTrue forKey:kCATransactionDisableActions];
self.arrowImage.hidden = YES;
[CATransaction commit];
break;
default:
break;
}
state_ = state;
}
#pragma mark - ScrollView Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.state == PullRefreshLoading) {
CGFloat offset = MAX(scrollView.contentOffset.y * -1, 0);
offset = MIN(offset, 60);
scrollView.contentInset = UIEdgeInsetsMake(offset, 0.0f, 0.0f, 0.0f);
} else if (scrollView.isDragging) {
BOOL loading = NO;
if ([self.delegate respondsToSelector:@selector(refreshHeaderViewDataSourceIsLoading:)]) {
loading = [self.delegate refreshHeaderViewDataSourceIsLoading:self];
}
if (self.state == PullRefreshPulling && scrollView.contentOffset.y > -65.0f && scrollView.contentOffset.y < 0.0f && !loading) {
[self setState:PullRefreshNormal];
} else if (self.state == PullRefreshNormal && scrollView.contentOffset.y < -65.0f && !loading) {
[self setState:PullRefreshPulling];
}
if (scrollView.contentInset.top != 0) {
scrollView.contentInset = UIEdgeInsetsZero;
}
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView {
BOOL loading = NO;
if ([self.delegate respondsToSelector:@selector(refreshHeaderViewDataSourceIsLoading:)]) {
loading = [self.delegate refreshHeaderViewDataSourceIsLoading:self];
}
if (scrollView.contentOffset.y <= -65.0f && !loading) {
if ([self.delegate respondsToSelector:@selector(refreshHeaderViewDidTriggerRefresh:)]) {
[self.delegate refreshHeaderViewDidTriggerRefresh:self];
}
self.state = PullRefreshLoading;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
scrollView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);
[UIView commitAnimations];
}
}
- (void)dataSourceDidFinishLoading:(UIScrollView *)scrollView {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.3];
[scrollView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
[UIView commitAnimations];
self.state = PullRefreshNormal;
}
#pragma mark - Dealloc
- (void)dealloc {
self.delegate = nil;
self.activityView = nil;
self.instructionLabel = nil;
self.arrowImage = nil;
self.statusLabel = nil;
[super dealloc];
}
@end