iOS开发知识点3——键盘
代码语言:javascript复制点击屏幕回收键盘是很简单的,但是在scrollView上点击回收键盘,直接调用那个方法就不能实现了
// 我的实现是这样的
// 首先实现一个继承自UIScrollView的Category,.m文件的实现
#import "UIScrollView UITouch.h"
@implementation UIScrollView (UITouch)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesBegan:touches withEvent:event];
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesMoved:touches withEvent:event];
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesEnded:touches withEvent:event];
[super touchesEnded:touches withEvent:event];
}
// 然后在要回收键盘的界面,导入这个类
#import "UIScrollView UITouch.h"
// 在touchesBegan方法里,得到要释放的textField,调用resignFirstResponder方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
MKInputPhoneNumberCell *inputPhoneNumberCell = (MKInputPhoneNumberCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[inputPhoneNumberCell.inputPhoneNumberTF resignFirstResponder];
MKPhoneCertifyTableViewCell *phoneCertifyCell = (MKPhoneCertifyTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
[phoneCertifyCell.inputCertifyTF resignFirstResponder];
}
@end
代码语言:javascript复制当键盘弹出时,有可能会遮盖住输入框,之前我采用把View放到scrollView上来处理,但是后来发现,让View跟着键盘动起来效果更好
// 首先注册通知,弹出键盘和键盘回收两个
// 弹出键盘时view向上偏移,避免遮盖输入框
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
// 然后实现通知的方法
#pragma mark - keyboard notification -
- (void)keyboardWillShow:(NSNotification *)note {
NSDictionary *info = [note userInfo];
// keyboardHeight 为键盘高度
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
[self animateViewWithKeyboardHeight: keyboardSize.height];
}
- (void)keyboardWillHide:(NSNotification *)note {
[self animateViewWithKeyboardHeight: 0.0];
}
- (void)animateViewWithKeyboardHeight:(CGFloat)keyboardHeight
{
NSTimeInterval animationDuration = 0.3f;
CGFloat width = self.bounds.size.width;
CGFloat height = self.bounds.size.height;
// 保持键盘和输入框底部view至少有-10的间距,这里的间距自己来定
CGFloat space = -10;
CGFloat topSize = 0.0;
CGFloat viewH = _bottomBackView.frame.size.height _bottomBackView.frame.origin.y space; // 得到view底部的y
CGFloat deviceH = self.bounds.size.height;
// 屏幕高度 - (view底部的y 键盘的高度), 如果>=0,则说明距离足够,设置view.origin.y = 0;否则则说明view需要上移
CGFloat animateH = deviceH - viewH - keyboardHeight;
if (animateH >= 0) {
topSize = 0;
CGRect toRect = CGRectMake(0, topSize, width, height);
self.frame = toRect;
} else {
topSize = animateH;
CGRect toRect = CGRectMake(0, topSize, width, height);
[UIView animateWithDuration:animationDuration animations:^{
self.frame = toRect;
}];
}
}
// 最后注销通知
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}