iOS 获取键盘下落速度

使自己创建的View根据键盘的显示和下落而改变frame,这需要使用iOS的通知机制,首先需要在通知中心注册

1     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
2     
3     [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];

下面是绑定的方法代码

- (void) keyboardWasShown:(NSNotification *) notif
{
        _tableView.frame=CGRectMake(0, -110, MAIN_WIDTH, MAIN_HRIGHT);
}

- (void) keyboardWasHidden:(NSNotification *) notif
{
    _tableView.frame=CGRectMake(0, 0, MAIN_WIDTH, MAIN_HRIGHT);
}

上面的代码是没有写动画效果的,但是却可以实现和键盘同速率的改变frame,这是一个很棒的功能

如果不放心的话,可以自己手动写动画效果,下面是代码

- (void) keyboardWasShown:(NSNotification *) notif
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:[[notif.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
    [UIView setAnimationCurve:[[notif.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue]];
    _tableView.frame=CGRectMake(0, -110, MAIN_WIDTH, MAIN_HRIGHT);
    [UIView commitAnimations];
}
原文地址:https://www.cnblogs.com/chebaodaren/p/4615718.html