iOS 监听键盘事件

注册通知监听器,监听键盘弹起事件

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

注册通知监听器,监听键盘收起事件

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

键盘弹出调用该方法

- (void)keyboardWillShow:(NSNotification *)notificationP{
    //获取键盘弹出的高度
    NSDictionary *dict = [notificationP userInfo];
    NSValue *value = [dict objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect key = [value CGRectValue];
    NSLog(@"%lf",key.size.height);
}

键盘收起调用该方法

- (void)keyboardWillHide:(NSNotification *)notificationP{
     
}

开始视图升起动画

 [UIView beginAnimations:@"keyboardWillShow" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//获取原视图位置
CGRect rect = viewHua.frame;
rect.origin.y = 60;
//设置视图位置
viewHua.frame = rect;
//结束动画
[UIView commitAnimations];

开始视图下降动画

[UIView beginAnimations:@"keyboardWillHide" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//获取原视图位置
CGRect rect = viewHua.frame;
rect.origin.y = HEIGHT-250;
//设置视图位置
viewHua.frame = rect;
//结束动画
[UIView commitAnimations];

你可能感兴趣的:(iOS 监听键盘事件)