-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollViewKeyboardHandler.m
More file actions
173 lines (145 loc) · 5.95 KB
/
ScrollViewKeyboardHandler.m
File metadata and controls
173 lines (145 loc) · 5.95 KB
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
//
// ScrollViewKeyboardHandler.m
// WinePicker
//
// Created by Alexei on 19.10.15.
// Copyright © 2015 Onix-Systems. All rights reserved.
//
#import "ScrollViewKeyboardHandler.h"
@interface ScrollViewKeyboardHandler()
@property (nonatomic, weak) UITapGestureRecognizer *tapGestureRecognizer;
@property (nonatomic, weak) UIView *viewToDim;
@property (nonatomic, strong) NSArray *viewsToDim;
@property (nonatomic, weak) UIView *dimView;
@property (nonatomic, weak) UITextField *textField;
@property (nonatomic) BOOL subscribed;
@end
@implementation ScrollViewKeyboardHandler
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)init {
self = [super init];
if (self) {
self.shouldAdjustScrollViewInsets = YES;
}
return self;
}
- (void)bringDimViewToFront {
[self.dimView.superview bringSubviewToFront:self.dimView];
}
- (void)setViewToDim:(UIView *)view fromTextFieldEntry:(UITextField *)textField {
self.viewToDim = view;
self.viewsToDim = nil;
[self reloadTextFieldTarget:textField];
}
- (void)setViewsToDim:(NSArray *)views fromTextFieldEntry:(UITextField *)textField {
self.viewToDim = nil;
self.viewsToDim = views;
[self reloadTextFieldTarget:textField];
}
- (void)reloadTextFieldTarget:(UITextField *)textField {
self.textField = textField;
[textField removeTarget:self action:@selector(textFieldTextChanged:) forControlEvents:UIControlEventEditingChanged];
[textField addTarget:self action:@selector(textFieldTextChanged:) forControlEvents:UIControlEventEditingChanged];
}
- (void)textFieldTextChanged:(UITextField *)textField {
if (textField.text.length) {
[self removeDim];
} else {
[self addDim];
}
}
- (BOOL)shouldAddDim {
return self.textField.text.length == 0;
}
- (void)tap:(id)sender {
[_scrollView.window endEditing:YES];
}
- (void)subscribe {
if (!self.subscribed) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
self.subscribed = YES;
}
}
- (void)unsubscribe {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
self.subscribed = NO;
}
- (void)addDim {
if (!self.dimView && (self.viewToDim || self.viewsToDim) && [self shouldAddDim]) {
UIControl *dimView = [[UIControl alloc] initWithFrame:CGRectZero];
dimView.backgroundColor = [UIColor blackColor];
dimView.alpha = 0;
[dimView addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
self.dimView = dimView;
if (self.viewToDim) {
[self.viewToDim.superview addSubview:dimView];
[dimView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.viewToDim);
}];
} else {
//Checking if superview is same on all views
UIView *top = self.viewsToDim[0];
UIView *right = self.viewsToDim[1];
UIView *bottom = self.viewsToDim[2];
UIView *left = self.viewsToDim[3];
[top.superview addSubview:dimView];
[dimView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(top);
make.trailing.equalTo(right);
make.bottom.equalTo(bottom);
make.leading.equalTo(left);
}];
}
[UIView animateWithDuration:0.2 animations:^{
dimView.alpha = 0.3;
}];
}
}
- (void)removeDim {
if (self.dimView) {
[UIView animateWithDuration:0.2 animations:^{
self.dimView.alpha = 0;
} completion:^(BOOL finished) {
[self.dimView removeFromSuperview];
}];
}
}
- (void)keyboardWillShow:(NSNotification *)notification {
//get the end position keyboard frame
[self addDim];
if (self.shouldAdjustScrollViewInsets) {
NSDictionary *keyInfo = [notification userInfo];
CGRect keyboardFrame = [[keyInfo objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
//convert it to the same view coords as the scrollView it might be occluding
keyboardFrame = [self.scrollView convertRect:keyboardFrame fromView:nil];
//calculate if the rects intersect
CGRect intersect = CGRectIntersection(keyboardFrame, self.scrollView.bounds);
if (!CGRectIsNull(intersect)) {
//yes they do - adjust the insets on scrollView to handle it
//first get the duration of the keyboard appearance animation
NSTimeInterval duration = [[keyInfo objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
//change the table insets to match - animated to the same duration of the keyboard appearance
[UIView animateWithDuration:duration animations:^{
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, intersect.size.height, 0);
self.scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, intersect.size.height, 0);
}];
}
}
}
- (void) keyboardWillHide: (NSNotification *) notification{
[self removeDim];
if (self.shouldAdjustScrollViewInsets) {
NSDictionary *keyInfo = [notification userInfo];
NSTimeInterval duration = [[keyInfo objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
//clear the table insets - animated to the same duration of the keyboard disappearance
[UIView animateWithDuration:duration animations:^{
self.scrollView.contentInset = UIEdgeInsetsZero;
self.scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}];
}
}
@end