Last active
October 11, 2015 08:47
-
-
Save i03nomura1y/3833020 to your computer and use it in GitHub Desktop.
iOS keyboard notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)setKeyboardNotification{ | |
NSNotificationCenter *c = [NSNotificationCenter defaultCenter]; | |
[c addObserver:self selector:@selector(notifKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; | |
[c addObserver:self selector:@selector(notifKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; | |
} | |
- (void)unsetKeyboardNotification { | |
NSNotificationCenter *c = [NSNotificationCenter defaultCenter]; | |
[c removeObserver:self name:UIKeyboardWillShowNotification object:nil]; | |
[c removeObserver:self name:UIKeyboardWillHideNotification object:nil]; | |
} | |
- (void)notifKeyboardWillShow:(NSNotification *)aNotification { | |
NSDictionary* info = aNotification.userInfo; | |
// この view から見た座標系に変換する | |
CGRect keyboardFrameEnd = [self.view.window convertRect:[info[UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:self.view]; | |
[UIView animateWithDuration:[info[UIKeyboardAnimationDurationUserInfoKey] floatValue] | |
delay:0 | |
options:[info[UIKeyboardAnimationCurveUserInfoKey] intValue] | |
animations:^{ | |
self.lc_bottom.constant = self.view.bounds.size.height - keyboardFrameEnd.origin.y; | |
[self.view layoutIfNeeded]; | |
} | |
completion:^(BOOL finished){ | |
} | |
]; | |
} | |
- (void)notifKeyboardWillHide:(NSNotification *)aNotification { | |
NSDictionary* info = aNotification.userInfo; | |
[UIView animateWithDuration:[info[UIKeyboardAnimationDurationUserInfoKey] floatValue] | |
delay:0 | |
options:[info[UIKeyboardAnimationCurveUserInfoKey] intValue] | |
animations:^{ | |
self.lc_bottom.constant = 0; | |
[self.view layoutIfNeeded]; | |
} | |
completion:^(BOOL finished){ | |
} | |
]; | |
} | |
- (void)closeSoftKeyboard:(UIGestureRecognizer*)recog { | |
[self.view endEditing:YES]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment