博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS全局处理键盘事件
阅读量:6328 次
发布时间:2019-06-22

本文共 2432 字,大约阅读时间需要 8 分钟。

  1. 注册监听键盘事件的通知
    [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(keyboardWillShow:)                                                 name:UIKeyboardWillShowNotification                                               object:nil];        [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(keyboardShow:)                                                 name:UIKeyboardDidShowNotification                                               object:nil];        [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(keyboardWillHide:)                                                 name:UIKeyboardWillHideNotification                                               object:nil];        [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(keyboardHide:)                                                 name:UIKeyboardDidHideNotification                                               object:nil];
  2. 在键盘将要出现和隐藏的回调中,加入动画。
    - (void)keyboardWillShow:(NSNotification *)notif {    if (self.hidden == YES) {        return;    }        CGRect rect = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];    CGFloat y = rect.origin.y;    [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:0.25];    NSArray *subviews = [self subviews];    for (UIView *sub in subviews) {                CGFloat maxY = CGRectGetMaxY(sub.frame);        if (maxY > y - 2) {            sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, sub.center.y - maxY + y - 2);        }    }    [UIView commitAnimations];}- (void)keyboardShow:(NSNotification *)notif {    if (self.hidden == YES) {        return;    }}- (void)keyboardWillHide:(NSNotification *)notif {    if (self.hidden == YES) {        return;    }    [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:0.25];    NSArray *subviews = [self subviews];    for (UIView *sub in subviews) {        if (sub.center.y < CGRectGetHeight(self.frame)/2.0) {            sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);        }    }    [UIView commitAnimations];}- (void)keyboardHide:(NSNotification *)notif {    if (self.hidden == YES) {        return;    }}

转载地址:http://wczoa.baihongyu.com/

你可能感兴趣的文章
mysqldump导出--数据+结构+(函数+存储过程)
查看>>
浏览器的渲染原理简介
查看>>
使用window.performance分析web前端性能
查看>>
获取系统当前时间参数date
查看>>
MySQL性能优化的最佳20+条经验
查看>>
exchange server 相关
查看>>
centos7系列安装vnc服务并授权用户访问
查看>>
CentOS mailx client
查看>>
字符串格式化
查看>>
Why Should You Choose Linux?
查看>>
NetScaler 12.1 发布
查看>>
checkpoint system management
查看>>
CentOS 6.5安全加固及性能优化_操作系统
查看>>
每天laravel-20160709|CallEvent
查看>>
我的友情链接
查看>>
【三石jQuery视频教程】02.创建 FontAwesome 复选框和单选框
查看>>
Cisco 配置DHCP中继 代理工程 实例
查看>>
Centos7.3部署KVM虚拟化环境
查看>>
configure: error: Cannot find ldap.h
查看>>
Linux启动分析(2)— bootsect.S、setup.S、head.S分析
查看>>