1.注册监听键盘事件的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardHide:)
name:UIKeyboardDidHideNotification
object:nil];
2. 在键盘将要出现和隐藏的回调中,加入动画。
- (void)keyboardWillShow:(NSNotification *)notif{
[UIView animateWithDuration:0.5f animations:^{
self.view.center = CGPointMake(160, 40);
}];
}
- (void)keyboardShow:(NSNotification *)notif {
}
- (void)keyboardWillHide:(NSNotification *)notif{
[UIView animateWithDuration:0.5f animations:^{
self.view.center = CGPointMake(160, 250);
}];
}
- (void)keyboardHide:(NSNotification *)notif {
}