9
9
#import " UIScrollView+ESFloatingView.h"
10
10
#import < objc/runtime.h>
11
11
12
- #define kScreenHeight [UIScreen mainScreen ].bounds.size.height
13
- #define kKeyWindow [UIApplication sharedApplication ].keyWindow
12
+ #define ScreenHeight [UIScreen mainScreen ].bounds.size.height
13
+ #define KeyWindow [UIApplication sharedApplication ].keyWindow
14
14
15
15
static char KEY_ES_floatingView;
16
- static char KEY_ES_isFloating;
17
- static char KEY_displayLink;
18
- static char KEY_targetOffsetY;
19
-
20
- @interface UIScrollView ()
21
-
22
- @property (nonatomic , assign ) CGFloat targetOffsetY;
23
- @property (strong , nonatomic ) CADisplayLink *displayLink;
24
-
25
- @end
26
16
27
17
@implementation UIScrollView (ESFloatingView)
28
18
@@ -41,87 +31,48 @@ + (void)load
41
31
- (instancetype )ESFloatView_initWithFrame : (CGRect )frame
42
32
{
43
33
UIScrollView *instance = [self ESFloatView_initWithFrame: frame];
44
- [[ NSNotificationCenter defaultCenter ] addObserver: self selector: @selector ( ESFloatView_ReceivedNotification_KeyboardWillShow: ) name: UIKeyboardWillShowNotification object: nil ];
34
+ [instance ESFloatView_setup ];
45
35
return instance;
46
36
}
47
37
48
38
- (instancetype )ESFloatView_initWithCoder : (NSCoder *)coder
49
39
{
50
40
UIScrollView *instance = [self ESFloatView_initWithCoder: coder];
51
- [[ NSNotificationCenter defaultCenter ] addObserver: self selector: @selector ( ESFloatView_ReceivedNotification_KeyboardWillShow: ) name: UIKeyboardWillShowNotification object: nil ];
41
+ [instance ESFloatView_setup ];
52
42
return instance;
53
43
}
54
44
45
+ - (void )ESFloatView_setup
46
+ {
47
+ [[NSNotificationCenter defaultCenter ] addObserver: self selector: @selector (ESFloatView_ReceivedNotification_KeyboardWillShow: ) name: UIKeyboardWillShowNotification object: nil ];
48
+ }
49
+
55
50
56
51
- (void )ESFloatView_ReceivedNotification_KeyboardWillShow : (NSNotification *)notification
57
52
{
53
+ self.autoresizesSubviews = NO ;
58
54
CGFloat keyboardH = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue ].size .height ;
59
55
60
56
if (self.ES_floatingView == NULL )
61
57
{
62
58
return ;
63
59
}
64
60
65
- CGRect absFrame = [self .ES_floatingView.superview convertRect: self .ES_floatingView.frame toView: kKeyWindow ];
61
+ CGRect absFrame = [self .ES_floatingView.superview convertRect: self .ES_floatingView.frame toView: KeyWindow ];
66
62
67
- CGFloat benchmarkY = kScreenHeight - keyboardH - absFrame.size .height ;
63
+ CGFloat benchmarkY = ScreenHeight - keyboardH - absFrame.size .height ;
68
64
69
65
CGFloat tableVOffset = self.contentOffset .y - (benchmarkY - absFrame.origin .y );
70
66
71
-
72
- self.targetOffsetY = tableVOffset;
73
-
74
- if (self.contentOffset .y != self.targetOffsetY ) {
75
- [self .displayLink addToRunLoop: [NSRunLoop currentRunLoop ] forMode: NSDefaultRunLoopMode ];
76
- }
77
- }
78
-
79
- - (void )ESFloatView_scrollToOffset
80
- {
81
- if (self.contentOffset .y - self.targetOffsetY < 2 && self.contentOffset .y - self.targetOffsetY > -2 )
82
- {
83
- CGPoint tmpOffset = self.contentOffset ;
84
- tmpOffset.y = self.targetOffsetY ;
85
- self.contentOffset = tmpOffset;
86
-
87
- [self .displayLink invalidate ];
88
- self.displayLink = nil ;
89
-
90
- self.ES_isFloating = NO ;
67
+ if (tableVOffset < -self.contentInset .top ) {
91
68
return ;
92
69
}
93
70
94
- self.ES_isFloating = YES ;
95
71
CGPoint tmpOffset = self.contentOffset ;
96
- tmpOffset.y += (self. targetOffsetY - self. contentOffset . y ) * 0.15 ;
72
+ tmpOffset.y = tableVOffset ;
97
73
self.contentOffset = tmpOffset;
98
74
}
99
75
100
- - (void )setTargetOffsetY : (CGFloat )targetOffsetY
101
- {
102
- objc_setAssociatedObject (self, &KEY_targetOffsetY, @(targetOffsetY), OBJC_ASSOCIATION_RETAIN_NONATOMIC );
103
- }
104
-
105
-
106
- - (CGFloat )targetOffsetY
107
- {
108
- return [objc_getAssociatedObject (self , &KEY_targetOffsetY) doubleValue ];
109
- }
110
-
111
- - (void )setDisplayLink : (CADisplayLink *)displayLink
112
- {
113
- objc_setAssociatedObject (self, &KEY_displayLink, displayLink, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
114
- }
115
-
116
- - (CADisplayLink *)displayLink
117
- {
118
- if (objc_getAssociatedObject (self, &KEY_displayLink) == NULL ) {
119
- CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget: self selector: @selector (ESFloatView_scrollToOffset )];
120
- objc_setAssociatedObject (self, &KEY_displayLink, displayLink, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
121
- }
122
-
123
- return objc_getAssociatedObject (self, &KEY_displayLink);
124
- }
125
76
126
77
- (void )setES_floatingView : (UIView *)floatView
127
78
{
@@ -133,16 +84,5 @@ - (UIView *)ES_floatingView
133
84
return objc_getAssociatedObject (self, &KEY_ES_floatingView);
134
85
}
135
86
136
- - (void )setES_isFloating : (BOOL )ES_isFloating
137
- {
138
- objc_setAssociatedObject (self, &KEY_ES_isFloating, @(ES_isFloating), OBJC_ASSOCIATION_RETAIN_NONATOMIC );
139
- }
140
-
141
- - (BOOL )ES_isFloating
142
- {
143
- return [objc_getAssociatedObject (self , &KEY_ES_isFloating) boolValue ];
144
- }
145
-
146
-
147
87
148
88
@end
0 commit comments