Permalink
Browse files

Added KVO: so that multiple DAKeyboardControl instances can all react…

… to keyboard frame changes
  • Loading branch information...
1 parent 005d1c4 commit 31b3687a3aa1c36b3754e54ea7687d36b9e89084 @danielamitay committed Nov 2, 2012
Showing with 38 additions and 6 deletions.
  1. +2 −1 .gitignore
  2. +36 −5 DAKeyboardControl/DAKeyboardControl.m
View
3 .gitignore
@@ -49,4 +49,5 @@ build/*
xcuserdata
profile
*.moved-aside
-DerivedData
+DerivedData
+*.xcuserstate
View
41 DAKeyboardControl/DAKeyboardControl.m
@@ -303,6 +303,22 @@ - (void)inputKeyboardDidHide:(NSNotification *)notification
self.keyboardActiveView = nil;
}
+- (void)observeValueForKeyPath:(NSString *)keyPath
+ ofObject:(id)object
+ change:(NSDictionary *)change
+ context:(void *)context
+{
+ if([keyPath isEqualToString:@"frame"] && object == self.keyboardActiveView)
+ {
+ CGRect keyboardEndFrameWindow = [[object valueForKeyPath:keyPath] CGRectValue];
+ CGRect keyboardEndFrameView = [self convertRect:keyboardEndFrameWindow fromView:self.keyboardActiveView.window];
+ if (self.keyboardDidMoveBlock && !self.keyboardActiveView.hidden)
+ {
+ self.keyboardDidMoveBlock(keyboardEndFrameView);
+ }
+ }
+}
+
#pragma mark - Touches Management
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
@@ -373,16 +389,18 @@ - (void)panGestureDidChange:(UIPanGestureRecognizer *)gesture
// Only update if the frame has actually changed
if (newKeyboardViewFrame.origin.y != self.keyboardActiveView.frame.origin.y)
- {
- CGRect newKeyboardViewFrameInView = [self convertRect:newKeyboardViewFrame fromView:self.keyboardActiveView.window];
-
+ {
[UIView animateWithDuration:0.0f
delay:0.0f
options:UIViewAnimationOptionTransitionNone
animations:^{
[self.keyboardActiveView setFrame:newKeyboardViewFrame];
+ /* Unnecessary now, due to KVO on self.keyboardActiveView
+ CGRect newKeyboardViewFrameInView = [self convertRect:newKeyboardViewFrame
+ fromView:self.keyboardActiveView.window];
if (self.keyboardDidMoveBlock)
self.keyboardDidMoveBlock(newKeyboardViewFrameInView);
+ */
}
completion:^(BOOL finished){
}];
@@ -401,18 +419,22 @@ - (void)panGestureDidChange:(UIPanGestureRecognizer *)gesture
else
shouldRecede = YES;
- // If the keyboard has only been pushed down 44 pixels or has been panned upwards let it pop back up; otherwise, let it drop down
+ // If the keyboard has only been pushed down 44 pixels or has been
+ // panned upwards let it pop back up; otherwise, let it drop down
CGRect newKeyboardViewFrame = self.keyboardActiveView.frame;
newKeyboardViewFrame.origin.y = (!shouldRecede ? keyboardWindowHeight - keyboardViewHeight : keyboardWindowHeight);
- CGRect newKeyboardViewFrameInView = [self convertRect:newKeyboardViewFrame fromView:self.keyboardActiveView.window];
[UIView animateWithDuration:0.25f
delay:0.0f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self.keyboardActiveView setFrame:newKeyboardViewFrame];
+ /* Unnecessary now, due to KVO on self.keyboardActiveView
+ CGRect newKeyboardViewFrameInView = [self convertRect:newKeyboardViewFrame
+ fromView:self.keyboardActiveView.window];
if (self.keyboardDidMoveBlock)
self.keyboardDidMoveBlock(newKeyboardViewFrameInView);
+ */
}
completion:^(BOOL finished){
if (shouldRecede)
@@ -537,6 +559,15 @@ - (UIView *)keyboardActiveView
- (void)setKeyboardActiveView:(UIView *)keyboardActiveView
{
[self willChangeValueForKey:@"keyboardActiveView"];
+ [self.keyboardActiveView removeObserver:self
+ forKeyPath:@"frame"];
+ if (keyboardActiveView)
+ {
+ [keyboardActiveView addObserver:self
+ forKeyPath:@"frame"
+ options:0
+ context:NULL];
+ }
objc_setAssociatedObject(self,
&UIViewKeyboardActiveView,
keyboardActiveView,

0 comments on commit 31b3687

Please sign in to comment.