-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDraggableKeyboardVC.m
204 lines (175 loc) · 6.27 KB
/
DraggableKeyboardVC.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
//
// DraggableKeyboardVC.m
// Instabank
//
// Created by Aleksey Kozhevnikov on 10.12.12.
// Copyright (c) 2012 iDa Mobile. All rights reserved.
//
#import "DraggableKeyboardVC.h"
#import "UIView+iOsUtils.h"
@interface DraggableKeyboardVC()
@property(nonatomic, assign) CGRect keyboardSuperFrame;
@property(nonatomic, weak) UIView* keyboardSuperView;
@property(nonatomic, assign) BOOL keyboardIsMoving;
@property(nonatomic, assign) BOOL blockGesture;
@end
@implementation DraggableKeyboardVC
+(void)addDummyInputAccessoryView:(id)textFieldOrView
{
if( [textFieldOrView isKindOfClass:[UITextField class]] || [textFieldOrView isKindOfClass:[UITextView class]] ) {
if( ![textFieldOrView inputAccessoryView] ) {
UIView* accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
accessoryView.backgroundColor = [UIColor clearColor];
[(id)textFieldOrView setInputAccessoryView:accessoryView];
}
}
}
-(void)setIsDraggingDisabled:(BOOL)isDraggingDisabled
{
NSAssert(!self.keyboardIsMoving, @"Property should not be changed while keyboard is dragging");
_isDraggingDisabled = isDraggingDisabled;
}
-(CGRect)openedKeyboardRect
{
return self.keyboardSuperFrame;
}
-(void)viewDidLoad
{
[super viewDidLoad];
self.draggingKeyboardPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
self.draggingKeyboardPanGestureRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:self.draggingKeyboardPanGestureRecognizer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)viewDidUnload
{
self.draggingKeyboardPanGestureRecognizer = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
self.keyboardSuperView.hidden = NO;
[super viewDidUnload];
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.keyboardSuperView.hidden = NO;
self.draggingKeyboardPanGestureRecognizer = nil;
}
#pragma mark - Keyboard Notifications
-(void)keyboardWillShow:(NSNotification*)notification
{
self.keyboardSuperView.hidden = NO;
self.blockGesture = NO;
}
-(void)keyboardWillHide:(NSNotification*)notification
{
self.blockGesture = YES;
}
-(void)keyboardDidShow:(NSNotification*)notification
{
UIResponder* firstResponder = [self.view findFirstResponderRecursively];
if( firstResponder ) {
self.keyboardSuperView = firstResponder.inputAccessoryView.superview;
self.keyboardSuperFrame = self.keyboardSuperView.frame;
}
}
#pragma mark - UIPanGestureRecognizer
-(void)panGesture:(UIPanGestureRecognizer*)gestureRecognizer
{
if( !self.keyboardSuperView.superview || self.isDraggingDisabled || self.blockGesture ) {
return;
}
if( gestureRecognizer.state == UIGestureRecognizerStateEnded && self.keyboardIsMoving ) {
const CGFloat keyboardShift = self.keyboardSuperView.y - self.keyboardSuperFrame.origin.y;
if( keyboardShift > (self.keyboardSuperFrame.size.height / 3.) ) {
[self hideDisplacedKeyboard];
} else {
[self bringBackDisplacedKeyboard];
}
self.keyboardIsMoving = NO;
} else {
CGFloat yLocationInKeyboard = [gestureRecognizer locationInView:self.keyboardSuperView].y;
CGFloat newKeyboardY = MAX(self.keyboardSuperFrame.origin.y, self.keyboardSuperView.y + yLocationInKeyboard);
newKeyboardY = MIN(self.keyboardSuperFrame.origin.y + self.keyboardSuperFrame.size.height, newKeyboardY);
CGFloat shift = (newKeyboardY - self.keyboardSuperView.y);
if( (newKeyboardY > self.keyboardSuperFrame.origin.y) && !self.keyboardIsMoving ) {
self.keyboardIsMoving = YES;
[self didShiftKeyboardViewBegin];
}
if( self.keyboardIsMoving && (shift != 0) ) {
self.keyboardSuperView.y += shift;
[self didShiftKeyboardViewContinue:shift];
}
}
}
-(void)hideDisplacedKeyboard
{
CGFloat animationDuration = 0.3;
[self willHideDisplacedKeyboardAnimatedWithDuration:animationDuration];
[UIView animateWithDuration:animationDuration
animations:^
{
self.keyboardSuperView.y = [UIScreen mainScreen].bounds.size.height;
[self hideDisplacedKeyboardAnimation:animationDuration];
}
completion:^(BOOL finished)
{
self.keyboardSuperView.hidden = YES;
self.keyboardSuperView.frame = self.keyboardSuperFrame;
[self didHideDisplacedKeyboard];
[self.view resignFirstResponderRecursively];
}];
}
-(void)willHideDisplacedKeyboardAnimatedWithDuration:(CGFloat)animationDuration
{
}
-(void)hideDisplacedKeyboardAnimation:(CGFloat)animationDuration
{
}
-(void)didHideDisplacedKeyboard
{
}
-(void)bringBackDisplacedKeyboard
{
CGFloat animationDuration = 0.3;
[self willBringBackDisplacedKeyboard:animationDuration];
[UIView animateWithDuration:animationDuration
animations:^
{
self.keyboardSuperView.frame = self.keyboardSuperFrame;
[self bringBackDisplacedKeyboardAnimation:animationDuration];
}
completion:^(BOOL finished)
{
[self didBringBackDisplacedKeyboard];
}];
}
-(void)willBringBackDisplacedKeyboard:(CGFloat)animationDuration
{
}
-(void)bringBackDisplacedKeyboardAnimation:(CGFloat)animationDuration
{
}
-(void)didBringBackDisplacedKeyboard
{
}
-(void)didShiftKeyboardViewBegin
{
}
-(void)didShiftKeyboardViewContinue:(CGFloat)shift
{
}
#pragma mark -
@end