/
UIViewController+KeyboardAnimation.m
176 lines (144 loc) · 9.11 KB
/
UIViewController+KeyboardAnimation.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
//
// UIViewController+KeyboardAnimation.m
//
// Copyright (c) 2015 Anton Gaenko
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "UIViewController+KeyboardAnimation.h"
#import <objc/runtime.h>
static void *ANFrameChangesAnimationsBlockAssociationKey = &ANFrameChangesAnimationsBlockAssociationKey;
static void *ANAnimationsBlockAssociationKey = &ANAnimationsBlockAssociationKey;
static void *ANBeforeAnimationsBlockAssociationKey = &ANBeforeAnimationsBlockAssociationKey;
static void *ANAnimationsCompletionBlockAssociationKey = &ANAnimationsCompletionBlockAssociationKey;
@implementation UIViewController (KeyboardAnimation)
#pragma mark public
- (void)an_subscribeKeyboardFrameChangesWithAnimations:(ANFrameChangesAnimationsWithKeyboardBlock)animations {
objc_setAssociatedObject(self, ANFrameChangesAnimationsBlockAssociationKey, animations, OBJC_ASSOCIATION_COPY_NONATOMIC);
// subscribe to notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(an_handleWillChageFrameKeyboardNotification:)
name:UIKeyboardWillChangeFrameNotification
object:nil];
}
- (void)an_subscribeKeyboardShowHideWithAnimations:(ANAnimationsWithKeyboardBlock)animations {
[self an_subscribeKeyboardShowHideWithAnimations:animations completion:nil];
}
- (void)an_subscribeKeyboardShowHideWithAnimations:(ANAnimationsWithKeyboardBlock)animations
completion:(ANCompletionKeyboardAnimations)completion {
[self an_subscribeKeyboardWithAnimations:animations completion:completion];
}
- (void)an_subscribeKeyboardWithAnimations:(ANAnimationsWithKeyboardBlock)animations
completion:(ANCompletionKeyboardAnimations)completion {
[self an_subscribeKeyboardWithBeforeAnimations:nil animations:animations completion:completion];
}
- (void)an_subscribeKeyboardShowHideWithBeforeAnimations:(ANBeforeAnimationsWithKeyboardBlock)beforeAnimations
animations:(ANAnimationsWithKeyboardBlock)animations
completion:(ANCompletionKeyboardAnimations)completion {
[self an_subscribeKeyboardShowHideWithBeforeAnimations:beforeAnimations
animations:animations
completion:completion];
}
- (void)an_subscribeKeyboardWithBeforeAnimations:(ANBeforeAnimationsWithKeyboardBlock)beforeAnimations
animations:(ANAnimationsWithKeyboardBlock)animations
completion:(ANCompletionKeyboardAnimations)completion {
// we shouldn't check for nil because it does nothing with nil
objc_setAssociatedObject(self, ANBeforeAnimationsBlockAssociationKey, beforeAnimations, OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, ANAnimationsBlockAssociationKey, animations, OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, ANAnimationsCompletionBlockAssociationKey, completion, OBJC_ASSOCIATION_COPY_NONATOMIC);
// subscribe to keyboard animations
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(an_handleWillShowKeyboardNotification:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(an_handleWillHideKeyboardNotification:)
name:UIKeyboardWillHideNotification
object:nil];
}
#pragma mark unsubscribe
- (void)an_unsubscribeKeyboard {
// remove assotiated blocks
objc_setAssociatedObject(self, ANBeforeAnimationsBlockAssociationKey, nil, OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, ANAnimationsBlockAssociationKey, nil, OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, ANAnimationsCompletionBlockAssociationKey, nil, OBJC_ASSOCIATION_COPY_NONATOMIC);
// unsubscribe from keyboard animations
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)an_unsubscribeKeyboardShowHide {
[self an_unsubscribeKeyboard];
}
- (void)an_unsubscribeKeyboardFrameChanges {
objc_setAssociatedObject(self, ANFrameChangesAnimationsBlockAssociationKey, nil, OBJC_ASSOCIATION_COPY_NONATOMIC);
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}
#pragma mark private
// ----------------------------------------------------------------
- (void)an_handleWillChageFrameKeyboardNotification:(NSNotification *)notification {
// getting keyboard animation attributes
CGRect keyboardRect = [self getKeyboardRectFromNotification:notification];
UIViewAnimationCurve curve = [self getAnimationCurveFromNotification:notification];
NSTimeInterval duration = [self getDurationFromNotification:notification];
// getting passed block
ANFrameChangesAnimationsWithKeyboardBlock animationsBlock = objc_getAssociatedObject(self, ANFrameChangesAnimationsBlockAssociationKey);
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[UIView setAnimationCurve:curve];
if (animationsBlock) animationsBlock(keyboardRect, duration);
}
completion:nil];
}
// ----------------------------------------------------------------
- (void)an_handleWillShowKeyboardNotification:(NSNotification *)notification {
[self an_keyboardWillShowHide:notification isShowing:YES];
}
// ----------------------------------------------------------------
- (void)an_handleWillHideKeyboardNotification:(NSNotification *)notification {
[self an_keyboardWillShowHide:notification isShowing:NO];
}
- (void)an_keyboardWillShowHide:(NSNotification *)notification isShowing:(BOOL)isShowing {
// getting keyboard animation attributes
CGRect keyboardRect = [self getKeyboardRectFromNotification:notification];
UIViewAnimationCurve curve = [self getAnimationCurveFromNotification:notification];
NSTimeInterval duration = [self getDurationFromNotification:notification];
// getting passed blocks
ANAnimationsWithKeyboardBlock animationsBlock = objc_getAssociatedObject(self, ANAnimationsBlockAssociationKey);
ANBeforeAnimationsWithKeyboardBlock beforeAnimationsBlock = objc_getAssociatedObject(self, ANBeforeAnimationsBlockAssociationKey);
ANCompletionKeyboardAnimations completionBlock = objc_getAssociatedObject(self, ANAnimationsCompletionBlockAssociationKey);
if (beforeAnimationsBlock) beforeAnimationsBlock(keyboardRect, duration, isShowing);
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[UIView setAnimationCurve:curve];
if (animationsBlock) animationsBlock(keyboardRect, duration, isShowing);
}
completion:completionBlock];
}
- (CGRect)getKeyboardRectFromNotification:(NSNotification *)notification {
return [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
}
- (UIViewAnimationCurve)getAnimationCurveFromNotification:(NSNotification *)notification {
return [[notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
}
- (NSTimeInterval)getDurationFromNotification:(NSNotification *)notification {
return [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
}
@end