监听键盘高度实现评论功能

效果展示:


代码:

//监听键盘出现和消失
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

#pragma mark 键盘出现
-(void)keyboardWillShow:(NSNotification *)note
{
// 这样就拿到了键盘的位置大小信息frame,然后根据frame进行高度处理之类的信息
NSDictionary *info = note.userInfo;
CGRect beginFrame = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
id animationDurationValue = [[note userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
float time = [animationDurationValue floatValue];
[UIView animateWithDuration:time animations:^{
if (self.commentTF.isFirstResponder && /*解决多次调用的问题,只执行最后一次*/beginFrame.size.height>0 && (beginFrame.origin.y-endFrame.origin.y>0)) {
self.toolView.mj_y = kSH-64-50*kAdaptValue - endFrame.size.height;
}
}];

}
#pragma mark 键盘消失
-(void)keyboardWillHide:(NSNotification *)note
{
if (self.commentTF.isFirstResponder) {
self.toolView.mj_y = kSH-64-50*kAdaptValue;
// flagToolHeight = 1;
}
}

别忘了。。。
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值