前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS开发知识点3——键盘

iOS开发知识点3——键盘

作者头像
莫空9081
发布2023-10-16 09:37:36
1830
发布2023-10-16 09:37:36
举报
文章被收录于专栏:iOS 备忘录

iOS开发知识点3——键盘

点击屏幕回收键盘是很简单的,但是在scrollView上点击回收键盘,直接调用那个方法就不能实现了

代码语言:javascript
复制
// 我的实现是这样的
// 首先实现一个继承自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

当键盘弹出时,有可能会遮盖住输入框,之前我采用把View放到scrollView上来处理,但是后来发现,让View跟着键盘动起来效果更好

代码语言:javascript
复制
// 首先注册通知,弹出键盘和键盘回收两个
// 弹出键盘时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];
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-11-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • iOS开发知识点3——键盘
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档