-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIViewController+BFScroll.m
211 lines (168 loc) · 6.19 KB
/
UIViewController+BFScroll.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
//
// UIViewController+BFScroll.m
//
// Created by wizardguy(Bigfeet) on 5/6/2016.
// Copyright © 2016 Dennis. All rights reserved.
//
// GitHub: https://github.com/wizardguy/BFScrollMenu
//
#import "UIViewController+BFScroll.h"
#import <objc/runtime.h>
@implementation UIViewController (BFScroll)
#pragma mark - properties
static const char BFConditionBlockKey = '\0';
- (void)setConditionBlock:(BFConditionBlock)conditionBlock
{
if (conditionBlock != self.conditionBlock) {
self.conditionBlock = nil;
objc_setAssociatedObject(self, &BFConditionBlockKey, conditionBlock, OBJC_ASSOCIATION_COPY);
}
}
- (BFConditionBlock)conditionBlock
{
return objc_getAssociatedObject(self, &BFConditionBlockKey);
}
static const char BFMenuViewKey = '\0';
- (void)setMenuView:(__kindof UIView *)menuView
{
if (menuView != self.menuView) {
self.menuView = nil;
objc_setAssociatedObject(self, &BFMenuViewKey, menuView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
}
- (__kindof UIView *)menuView
{
return objc_getAssociatedObject(self, &BFMenuViewKey);
}
static const char BFScrollActionBlockKey = '\0';
- (void)setScrollActionBlock:(BFActionBlock)scrollActionBlock
{
if (scrollActionBlock != self.scrollActionBlock) {
self.scrollActionBlock = nil;
objc_setAssociatedObject(self, &BFScrollActionBlockKey, scrollActionBlock, OBJC_ASSOCIATION_COPY);
}
}
- (BFActionBlock)scrollActionBlock
{
return objc_getAssociatedObject(self, &BFScrollActionBlockKey);
}
static const char BFStopActionBlockKey = '\0';
- (void)setStopActionBlock:(BFActionBlock)stopActionBlock
{
if (stopActionBlock != self.stopActionBlock) {
self.stopActionBlock = nil;
objc_setAssociatedObject(self, &BFStopActionBlockKey, stopActionBlock, OBJC_ASSOCIATION_COPY);
}
}
- (BFActionBlock)stopActionBlock
{
return objc_getAssociatedObject(self, &BFStopActionBlockKey);
}
static const char BFViewStateKey = '\0';
- (void) setViewState:(BFMenuViewState)viewState
{
objc_setAssociatedObject(self, &BFViewStateKey, [NSNumber numberWithInt:viewState], OBJC_ASSOCIATION_ASSIGN);
}
- (BFMenuViewState)viewState
{
id val = objc_getAssociatedObject(self, &BFViewStateKey);
return (val == nil) ? 0 : [(NSNumber *)val intValue];
}
static const char BFLastDirectionKey = '\0';
- (void)setLastDirection:(BFScrollingDirection)lastDirection
{
objc_setAssociatedObject(self, &BFLastDirectionKey, [NSNumber numberWithInt:lastDirection], OBJC_ASSOCIATION_ASSIGN);
}
- (BFScrollingDirection)lastDirection
{
id val = objc_getAssociatedObject(self, &BFLastDirectionKey);
return (val == nil) ? 0 : [(NSNumber *)val intValue];
}
static const char BFScrollingSensitivityKey = '\0';
- (void)setScrollingSensitivity:(int)scrollingSensitivity
{
objc_setAssociatedObject(self, &BFScrollingSensitivityKey, [NSNumber numberWithInt:scrollingSensitivity], OBJC_ASSOCIATION_ASSIGN);
}
- (int)scrollingSensitivity
{
id val = objc_getAssociatedObject(self, &BFScrollingSensitivityKey);
return (val == nil) ? 0 : [(NSNumber *)val intValue];
}
static const char BFLastPostionKey = '\0';
- (void)setLastPosition:(int)lastPosition
{
objc_setAssociatedObject(self, &BFLastPostionKey, [NSNumber numberWithInt:lastPosition], OBJC_ASSOCIATION_ASSIGN);
}
- (int)lastPosition
{
id val = objc_getAssociatedObject(self, &BFLastPostionKey);
return (val == nil) ? 0 : [(NSNumber *)val intValue];
}
#pragma mark - methods
- (void)setUpDelegateClass:(Class)delegateClass
MenuView:(UIView *)menuView
conditionBlock:(BFConditionBlock)condition
scrollActionBlock:(BFActionBlock)scrollAction
stopActionBlock:(BFActionBlock)stopAction
{
self.menuView = menuView;
self.conditionBlock = condition;
self.scrollActionBlock = scrollAction;
self.stopActionBlock = stopAction;
self.lastDirection = BFScrollingDirectionStill;
self.viewState = BFMenuViewStateUnknown;
self.lastPosition = 0;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
method_exchangeImplementations(class_getInstanceMethod(delegateClass, @selector(scrollViewDidScroll:)), class_getInstanceMethod([self class], @selector(BFScrollViewDidScroll:)));
if (self.stopActionBlock) {
method_exchangeImplementations(class_getInstanceMethod(delegateClass, @selector(scrollViewDidEndDecelerating:)), class_getInstanceMethod([self class], @selector(BFScrollViewDidEndDecelerating:)));
}
});
}
- (void)actionWithBlock:(BFActionBlock)block
{
if (block) {
// if state changes
BFMenuViewState currentState = (BFMenuViewState)self.lastDirection;
if (self.viewState != currentState) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// if condition matches
if ((self.conditionBlock && self.conditionBlock(self.lastDirection, self.lastPosition)) || self.conditionBlock == nil) {
// do the action
dispatch_async(dispatch_get_main_queue(), ^{
block(self.lastDirection);
self.viewState = currentState;
});
}
});
}
}
}
- (void)BFScrollViewDidScroll:(UIScrollView *)scrollView
{
[self BFScrollViewDidScroll:scrollView];
int currentPosition = scrollView.contentOffset.y;
BFScrollingDirection direction = self.lastDirection;
if (!scrollView.decelerating) {
int delta = currentPosition - self.lastPosition;
// Scrolling Up
if (delta > self.scrollingSensitivity) {
direction = BFScrollingDirectionUP;
}
// Scrolling Down
else if (delta < 0 && -delta > self.scrollingSensitivity) {
direction = BFScrollingDirectionDown;
delta = -delta;
}
}
direction != BFScrollingDirectionStill ? self.lastDirection = direction : NO;
self.lastPosition = currentPosition;
[self actionWithBlock:self.scrollActionBlock];
}
- (void)BFScrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self BFScrollViewDidEndDecelerating:scrollView];
[self actionWithBlock:self.stopActionBlock];
}
@end