iOS键盘监听方法

setAnimationDuration 方法:定义动画的时间。
setAnimationCurve 方法:定义动画的轨迹(动画如何执行)

 
typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
    UIViewAnimationCurveEaseInOut,         // 开始时慢,中间快,结束时慢
    UIViewAnimationCurveEaseIn,            // 开始慢,然后加速
    UIViewAnimationCurveEaseOut,           // 逐渐减速
    UIViewAnimationCurveLinear             // 匀速
};


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


-(void)keyboardChangeFrame:(NSNotification*)noti;
{
    
    CGRect rect= [noti.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
    NSLog(@"%@", noti.userInfo);
    CGFloat keyboardheight = rect.origin.y;
    CGFloat duration=[noti.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
    CGFloat curve=[noti.userInfo[UIKeyboardAnimationCurveUserInfoKey]doubleValue];
    [_messageTable zxp_updateConstraints:^(ZXPAutoLayoutMaker *layout) {
        layout.top.offset(0);
        layout.left.offset(0);
        layout.right.offset(0);
        layout.height.offset(keyboardheight-30-80);
    }];
    [_sendTextView zxp_updateConstraints:^(ZXPAutoLayoutMaker *layout) {
        layout.left.offset(0);
        layout.right.offset(0);
        layout.height.offset(45);
        layout.top.equalTo(_messageTable.zxp_bottom).offset(0);
    }];
    [UIView setAnimationCurve:curve];
    [UIView animateWithDuration:duration animations:^{
        [self.view layoutIfNeeded];
        [self tableScrollToBottom];
    } completion:^(BOOL finished) {
        
    }];
    
}


-(void)keyboardHide:(NSNotification *)noti
{
    [self.view endEditing:YES];

    [UIView animateWithDuration:0.3 animations:^{
            
        _inputView.frame = CGRectMake(0, ScreenViewWH-260-110, ScreenViewW, 260);
        [_inputView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.offset(0);
            make.right.offset(0);
            make.bottom.offset(-64);
            make.height.offset(260);
        }];
        [self.view layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        
    }];

}


最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容