-
Notifications
You must be signed in to change notification settings - Fork 21
/
AllAroundPullView.m
348 lines (304 loc) · 12.9 KB
/
AllAroundPullView.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//
// AllAroundPullView.m
//
// Created by r_plus on 9/2/12
// Copyright (c) 2012 r_plus All rights reserved.
//
// https://github.com/r-plus/AllAroundPullView
//
// based on twobitlabs modified version of PullToRefreshView by Grant Paul (chpwn)
//
#import "AllAroundPullView.h"
@interface AllAroundPullView ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) CALayer *arrowImage;
@property (nonatomic, assign) AllAroundPullViewState state;
@property (nonatomic, assign) UIEdgeInsets originalInset;
@property (nonatomic, copy) void (^allAroundPullViewActionHandler)(AllAroundPullView *view);
- (void)startTimer;
- (void)dismissView;
- (BOOL)isScrolledToVisible;
- (BOOL)isScrolledOverThreshold;
- (void)parkVisible;
- (void)hide;
- (id)initWithScrollView:(UIScrollView *)scroll position:(AllAroundPullViewPosition)position;
@end
@implementation AllAroundPullView
{
AllAroundPullViewState _state;
AllAroundPullViewPosition _position;
}
static const CGFloat kViewHeight = 60.0;
static const CGFloat kSidePullViewWidth = 60.0;
// dynamic ON/OFF set.
- (void)hideAllAroundPullViewIfNeed:(BOOL)disable
{
self.arrowImage.opacity = disable ? 0.0 : 1.0;
self.state = disable ? AllAroundPullViewStateNone : AllAroundPullViewStateNormal;
}
- (void)showActivity:(BOOL)shouldShow animated:(BOOL)animated
{
if (shouldShow)
[self.activityView startAnimating];
else
[self.activityView stopAnimating];
[UIView animateWithDuration:(animated ? 0.1 : 0.0) animations:^{
self.arrowImage.opacity = (shouldShow ? 0.0 : 1.0);
}];
}
- (void)setImageFlipped:(BOOL)flipped
{
if (self.isSideView) {
BOOL isLeft = (self.position == AllAroundPullViewPositionLeft);
[UIView animateWithDuration:0.1 animations:^{
self.arrowImage.transform = (flipped ^ isLeft ? CATransform3DMakeRotation(M_PI / 2.0, 0.0, 0.0, 1.0) : CATransform3DMakeRotation(M_PI / 2.0 * 3.0, 0.0, 0.0, 1.0));
}];
} else {
BOOL isBottom = (self.position == AllAroundPullViewPositionBottom);
[UIView animateWithDuration:0.1 animations:^{
self.arrowImage.transform = (flipped ^ isBottom ? CATransform3DMakeRotation(M_PI * 2, 0.0, 0.0, 1.0) : CATransform3DMakeRotation(M_PI, 0.0, 0.0, 1.0));
}];
}
}
- (id)initWithScrollView:(UIScrollView *)scroll position:(AllAroundPullViewPosition)position action:(void (^)(AllAroundPullView *view))actionHandler
{
self.allAroundPullViewActionHandler = actionHandler;
return [self initWithScrollView:scroll position:position];
}
- (void)layoutSubviews
{
CGFloat visibleBottom = self.position == AllAroundPullViewPositionBottom ? kViewHeight : self.frame.size.height;
CGFloat visibleLeft = self.position == AllAroundPullViewPositionLeft ? kSidePullViewWidth : self.frame.size.width;
if (self.isSideView)
self.arrowImage.position = CGPointMake(visibleLeft - kSidePullViewWidth + 30.0, self.frame.size.height / 2.0);
else
self.arrowImage.position = CGPointMake(self.frame.size.width / 2.0, visibleBottom - kViewHeight + 30.0);
}
- (id)initWithScrollView:(UIScrollView *)scroll position:(AllAroundPullViewPosition)position
{
CGFloat offset;
CGRect frame;
switch (position) {
case AllAroundPullViewPositionTop:
case AllAroundPullViewPositionBottom:
offset = (position == AllAroundPullViewPositionBottom) ? scroll.contentSize.height : 0.0 - scroll.bounds.size.height;
frame = CGRectMake(0.0, offset, scroll.bounds.size.width, scroll.bounds.size.height);
break;
case AllAroundPullViewPositionLeft:
case AllAroundPullViewPositionRight:
offset = (position == AllAroundPullViewPositionLeft) ? -kSidePullViewWidth : scroll.contentSize.width;
frame = CGRectMake(offset, 0.0, kSidePullViewWidth, scroll.frame.size.height);
break;
}
if ((self = [super initWithFrame:frame])) {
_position = position;
self.scrollView = scroll;
[self.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:NULL];
[self.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:NULL];
self.autoresizingMask = self.isSideView ? UIViewAutoresizingFlexibleHeight : UIViewAutoresizingFlexibleWidth;
self.backgroundColor = [UIColor clearColor];
_threshold = 60.0;
self.arrowImage = [[CALayer alloc] init];
UIImage *arrow = [UIImage imageNamed:@"arrow.png"];
self.arrowImage.contents = (id) arrow.CGImage;
CGRect arrowAndActivityFrame;
if (self.isSideView) {
CGFloat sideArrowPositionOffset = (self.position == AllAroundPullViewPositionLeft) ? kSidePullViewWidth - 10.0 : self.frame.size.width - 15.0;
arrowAndActivityFrame = CGRectMake(sideArrowPositionOffset - kSidePullViewWidth + 30.0, self.frame.size.height / 2.0 - 30.0, arrow.size.width, arrow.size.height);
} else {
CGFloat arrowPositionOffset = (self.position == AllAroundPullViewPositionBottom) ? kViewHeight : self.frame.size.height;
arrowAndActivityFrame = CGRectMake(self.frame.size.width / 2.0 - arrow.size.width / 2.0, arrowPositionOffset - kViewHeight + 5.0, arrow.size.width, arrow.size.height);
}
self.arrowImage.frame = arrowAndActivityFrame;
self.arrowImage.contentsGravity = kCAGravityResizeAspect;
[self setImageFlipped:NO];
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
self.arrowImage.contentsScale = [[UIScreen mainScreen] scale];
[self.layer addSublayer:self.arrowImage];
self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.activityView.autoresizingMask = self.isSideView ? (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin) : (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin);
self.activityView.frame = arrowAndActivityFrame;
[self addSubview:self.activityView];
self.state = AllAroundPullViewStateNormal;
}
return self;
}
- (void)updatePositionWhenContentsSizeIsChanged
{
if (self.position == AllAroundPullViewPositionBottom)
self.frame = CGRectMake(0.0, self.scrollView.contentSize.height, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
else if (self.position == AllAroundPullViewPositionRight) {
CGRect rect = self.frame;
rect.origin.x = self.scrollView.contentSize.width;
self.frame = rect;
}
}
- (void)updatePosition
{
if (self.isSideView) {
CGRect rect = self.frame;
rect.origin.y = self.scrollView.contentOffset.y;
self.frame = rect;
} else {
CGRect frame = self.frame;
frame.origin.x = self.scrollView.contentOffset.x;
self.frame = frame;
}
}
#pragma mark -
#pragma mark Setters
- (void)setState:(AllAroundPullViewState)state_
{
_state = state_;
switch (_state) {
case AllAroundPullViewStateReady:
[self showActivity:NO animated:NO];
[self setImageFlipped:YES];
break;
case AllAroundPullViewStateNormal:
[self showActivity:NO animated:NO];
[self setImageFlipped:NO];
break;
case AllAroundPullViewStateLoading:
[self showActivity:YES animated:YES];
[self setImageFlipped:NO];
[self parkVisible];
[self startTimer];
break;
default:
break;
}
}
#pragma mark -
#pragma mark Getters
- (BOOL)isSideView
{
if (self.position & (AllAroundPullViewPositionLeft | AllAroundPullViewPositionRight))
return YES;
else
return NO;
}
#pragma mark -
#pragma mark UIScrollView
- (BOOL)isScrolledToVisible
{
switch (self.position) {
case AllAroundPullViewPositionTop:
{
BOOL scrolledAboveContent = self.scrollView.contentOffset.y < 0.0;
return scrolledAboveContent && ![self isScrolledOverThreshold];
}
case AllAroundPullViewPositionBottom:
{
BOOL scrolledBelowContent = self.scrollView.contentOffset.y > (self.scrollView.contentSize.height - self.scrollView.frame.size.height);
return scrolledBelowContent && ![self isScrolledOverThreshold];
}
case AllAroundPullViewPositionLeft:
case AllAroundPullViewPositionRight:
return ![self isScrolledOverThreshold];
}
return NO;
}
- (BOOL)isScrolledOverThreshold
{
switch (self.position) {
case AllAroundPullViewPositionTop:
return self.scrollView.contentOffset.y <= -self.threshold;
case AllAroundPullViewPositionBottom:
if (self.scrollView.contentSize.height > self.scrollView.frame.size.height)
return self.scrollView.contentOffset.y >= (self.scrollView.contentSize.height - self.scrollView.frame.size.height) + self.threshold;
else
return self.scrollView.contentOffset.y >= self.threshold;
case AllAroundPullViewPositionLeft:
return self.scrollView.contentOffset.x <= -self.threshold;
case AllAroundPullViewPositionRight:
return self.scrollView.contentOffset.x >= (self.scrollView.contentSize.width - self.scrollView.frame.size.width) + self.threshold;
}
return NO;
}
- (void)parkVisible
{
self.originalInset = self.scrollView.contentInset;
if (self.position == AllAroundPullViewPositionTop)
self.scrollView.contentInset = UIEdgeInsetsMake(kViewHeight + self.originalInset.top, self.originalInset.left, self.originalInset.bottom, self.originalInset.right);
else if (self.position == AllAroundPullViewPositionBottom)
self.scrollView.contentInset = UIEdgeInsetsMake(self.originalInset.top, self.originalInset.left, kViewHeight + self.originalInset.bottom, self.originalInset.right);
}
- (void)hide
{
if (!self.isSideView)
self.scrollView.contentInset = self.originalInset;
}
- (void)handleDragWhileLoading
{
if ([self isScrolledOverThreshold] || [self isScrolledToVisible]) {
// allow scrolled portion of view to display
if (self.position == AllAroundPullViewPositionBottom) {
CGFloat visiblePortion = self.scrollView.contentOffset.y - (self.scrollView.contentSize.height - self.scrollView.frame.size.height);
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, MIN(visiblePortion, kViewHeight), 0);
} else if (self.position == AllAroundPullViewPositionTop) {
self.scrollView.contentInset = UIEdgeInsetsMake(MIN(-self.scrollView.contentOffset.y, kViewHeight), 0, 0, 0);
}
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"contentSize"])
[self updatePositionWhenContentsSizeIsChanged];
if ([keyPath isEqualToString:@"contentOffset"]) {
if (self.scrollView.isDragging) {
if (_state == AllAroundPullViewStateReady) {
if ([self isScrolledToVisible]) {
// dragging from "release to refresh" back down (didn't release at top)
[self setState:AllAroundPullViewStateNormal];
}
} else if (_state == AllAroundPullViewStateNormal) {
// hit the upper limit, change to "release to refresh"
if ([self isScrolledOverThreshold]) {
[self setState:AllAroundPullViewStateReady];
}
} else if (_state == AllAroundPullViewStateLoading) {
[self handleDragWhileLoading];
}
if (_state != AllAroundPullViewStateNone) {
[self updatePosition];
}
} else {
if (_state == AllAroundPullViewStateReady) {
[UIView animateWithDuration:0.2 animations:^{
[self setState:AllAroundPullViewStateLoading];
}];
if (self.allAroundPullViewActionHandler)
self.allAroundPullViewActionHandler(self);
}
}
}
}
- (void)finishedLoading
{
if (_state == AllAroundPullViewStateLoading)
[self dismissView];
}
- (void)dismissView
{
[UIView animateWithDuration:0.3 animations:^{
[self setState:AllAroundPullViewStateNormal];
[self hide];
}];
}
#pragma mark -
#pragma mark Timeout
- (void)startTimer
{
if (self.timeout > 0)
[self performSelector:@selector(dismissView) withObject:nil afterDelay:self.timeout];
}
#pragma mark -
#pragma mark Dealloc
- (void)dealloc
{
[self.scrollView removeObserver:self forKeyPath:@"contentOffset"];
[self.scrollView removeObserver:self forKeyPath:@"contentSize"];
}
@end
/* vim: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab ff=unix: */