第一个 监听键盘弹出高度 避免遮挡页面
网上找的 存起来
viewDidLoad里
// 监听键盘通知
[[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 *)showNot{
// 1.取出键盘frame
CGRect keyboardFrame= [showNot.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat upRectFrame = keyboardFrame.size.height /2;
// 2.键盘弹出的时间
CGFloat duration=[showNot.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 3.执行动画
[UIView animateWithDuration:duration animations:^{
self.view.transform=CGAffineTransformMakeTranslation(0,-upRectFrame);
}];
}
-(void)keyboardWillHide:(NSNotification *)hideNot{
// 2.键盘弹出的时间
CGFloat duration=[hideNot.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 3.执行动画
[UIView animateWithDuration:duration animations:^{
self.view.transform=CGAffineTransformIdentity;
}];
}
//释放通知
-(void)delete:(id)sender{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
点击空白 滑动 收起键盘
self.view.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
[self.view addGestureRecognizer:singleTap];
-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer
{
[self.view endEditing:YES];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self.view endEditing:YES];
}