OC中键盘的处理

作品链接:
//www.greatytc.com/users/1e0f5e6f73f6/top_articles

  • 使用通知的方法

1.监听键盘通知

- (void)viewDidLoad {
    [super viewDidLoad]; 
    // 监听键盘通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

2.取消通知

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

3.键盘处理

#pragma mark - 键盘处理
- (void)keyboardWillChangeFrame:(NSNotification *)note {
    // 取出键盘最终的frame
    CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    // 取出键盘弹出需要花费的时间
    double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    // 修改transform
    [UIView animateWithDuration:duration animations:^{
        // 平移多少  rect.origin.y  键盘高度
        CGFloat ty = [UIScreen mainScreen].bounds.size.height - rect.origin.y;
        self.view.transform = CGAffineTransformMakeTranslation(0, - ty);
    }];
}

4.退出键盘

#pragma mark - <UITableViewDelegate>
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 退出键盘
    [self.view endEditing:YES];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,482评论 25 708
  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一种新的协议。它实...
    香橙柚子阅读 24,117评论 8 183
  • 徒步318第6天 雅安早晨阴天,吃了早饭,7点左右出发,今天出师不利,出门没多久,遮阳帽丢了,好歹今天不用带,要不...
    智仁山水阅读 342评论 1 0
  • 我所有的好,你都享用;我所有的坏,他都挨着。 你说你知道的我爱你,可是他对我说我爱你。 你爱看着我笑,阳光就从你的...
    半颗心的鱼_0d22阅读 206评论 1 2
  • 三岛说,我们历代与时间交媾,在无尽的乱伦中世代延续。 骨架是肉体的框架,肉体由思想支配,吃喝为肉体的不衰提供能量,...
    小歌阅读 656评论 1 2