iOS 获取键盘弹出高度,动画时间和动画路径曲线

系统对键盘的活动有几个通知的key

UIKeyboardWillShowNotification//键盘将要弹起
UIKeyboardDidShowNotification//键盘已经弹起
UIKeyboardWillHideNotification//键盘将要收起
UIKeyboardDidHideNotification//键盘已经收起

我们可以通过这几个key来接收键盘活动的通知

//监听键盘活动
- (void)addNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

在通知的userinfo中包含了键盘动画的所有信息

notification.userInfo
{
    UIKeyboardAnimationCurveUserInfoKey = 7;//动画曲线类型  
    UIKeyboardAnimationDurationUserInfoKey = "0.25";//动画持续时间 
    UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 226}}";//键盘的bounds
    UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 849}";//键盘动画起始时的中心点  
    UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 623}";//键盘动画结束时的中心点  
    UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 736}, {414, 226}}";//键盘动画起始时的frame
    UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 510}, {414, 226}}";//键盘动画结束时的frame
    UIKeyboardIsLocalUserInfoKey = 1;//键盘是否显示,bool类型,1 show,2 hide
}
- (void)keyboardWillShow:(NSNotification *)notification {
    NSDictionary *userInfo = notification.userInfo;
    CGRect frame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];//键盘动画结束时的frame
    NSTimeInterval timeInterval = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];//动画持续时间
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];//动画曲线类型
    //your animation code
    __weak typeof(self) weakself = self;
    [UIView setAnimationCurve:curve];
    [UIView animateWithDuration:timeInterval animations:^{
        //code
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    NSDictionary *userInfo = notification.userInfo;
    CGRect frame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];//键盘动画结束时的frame
    NSTimeInterval timeInterval = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];//动画持续时间
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];//动画曲线类型
    //your animation code
    __weak typeof(self) weakself = self;
    [UIView setAnimationCurve:curve];
    [UIView animateWithDuration:timeInterval animations:^{
        //code
    }];
}

最后把上面代码中重复部分抽出来就好了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容