1.先設置監聽者以及監聽對象和事件
<pre>
<code class='objectivec hljs'>
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
2.實現監聽方法
(void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
// 獲取鍵盤的frame
CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 獲取鍵盤的動畫時間
NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
// do something
}(void)keyboardWillHide:(NSNotification *)notification {
// do something
}
</code></pre>