獲取iOS設(shè)備鍵盤高度和動(dòng)畫彈出時(shí)間
添加監(jiān)聽通知
如代碼所示,監(jiān)聽通知,并添加方法
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
實(shí)現(xiàn)通知響應(yīng)方法
注意獲取的是 UIKeyboardFrameEndUserInfoKey
,這個(gè)是變化后的值,如果獲取到的值不是你想要的,變化一下對(duì)應(yīng)的 KEY
直到取到你想要的值。
- (void)keyboardWillShow:(NSNotification *)notification
{
//獲取鍵盤的高度
NSDictionary *userInfo = [notification userInfo];
NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [value CGRectValue];
int height = keyboardRect.size.height;
//獲取動(dòng)畫時(shí)間
double animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//處理自己的業(yè)務(wù)邏輯
[self.infoView handleKeyboardWillShowWithHeight:height Duration:animationDuration];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
//獲取鍵盤的高度
NSDictionary *userInfo = [notification userInfo];
NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [value CGRectValue];
int height = keyboardRect.size.height;
//獲取動(dòng)畫時(shí)間
double animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//處理自己的業(yè)務(wù)邏輯
[self.infoView handleKeyboardWillHideWithHeight:height Duration:animationDuration];
}