1.注册键盘通知事件

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    // 键盘将出现事件监听
[center addObserver:self selector:@selector(handleKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; // 键盘将隐藏事件监听
[center addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

2.处理动画

// 键盘出现

- (void)handleKeyboardWillShow:(NSNotification *)paramNotification
{
NSDictionary *userInfo = [paramNotification userInfo]; NSValue *animationCurveObject = [userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey]; NSValue *animationDurationObject = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey]; NSValue *keyboardEndRectObject = [userInfo valueForKey:UIKeyboardFrameEndUserInfoKey]; NSUInteger animationCurve = ; double animationDuration = 0.0f; CGRect keyboardEndRect = CGRectMake(,, , ); [animationCurveObject getValue:&animationCurve]; [animationDurationObject getValue:&animationDuration]; [keyboardEndRectObject getValue:&keyboardEndRect]; UIWindow *window = [[[UIApplication sharedApplication] delegate] window]; CGRect intersectionOfKeyboardRectAndWindowRect = CGRectIntersection(window.frame, keyboardEndRect); CGFloat bottomInset = intersectionOfKeyboardRectAndWindowRect.size.height; [UIView beginAnimations:@"changeViewContentInset" context:NULL]; [UIView setAnimationDuration:animationDuration]; [UIView setAnimationCurve:(UIViewAnimationCurve) animationCurve]; CGRect tempRect = self.contentView.frame; tempRect.origin.y = Main_Screen_Height - bottomInset - - ; self.contentView.frame = tempRect; [UIView commitAnimations];
}
// 键盘消失

- (void)handleKeyboardWillHide:(NSNotification *)paramNotification
{
NSDictionary *userInfo = [paramNotification userInfo]; NSValue *animationCurveObject =[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey]; NSValue *animationDurationObject = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey]; NSValue *keyboardEndRectObject =[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey]; NSUInteger animationCurve = ; double animationDuration = 0.0f; CGRect keyboardEndRect = CGRectMake(, , , ); [animationCurveObject getValue:&animationCurve]; [animationDurationObject getValue:&animationDuration]; [keyboardEndRectObject getValue:&keyboardEndRect]; [UIView beginAnimations:@"changeViewContentInset" context:NULL]; [UIView setAnimationDuration:animationDuration]; [UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve]; self.contentView.center = self.center; [UIView commitAnimations];
}

3.当然是别忘了注销通知哦

[[NSNotificationCenter defaultCenter] removeObserver:self];

iOS键盘监听事件的更多相关文章

  1. js 获取当前焦点所在的元素、给元素和input控件添加键盘监听事件、添加页面级的键盘监听事件

    页面级的键盘监听事件 document.onkeydown = function (event) { var e = event || window.event || arguments.callee ...

  2. [置顶] java Gui 键盘监听事件

    简单写一个java Gui键盘监听事件,实现的效果就是按下键盘控制台输出你按下的键.比如:按下A控制台就输出A 效果如图: 以下把实现的效果分为几个步骤: 1.新建一个窗体类继承窗体: 2.给这个窗体 ...

  3. JPanel添加键盘监听事件

    因为在自己的游戏需求中谢了要用键盘控制飞机的移动,所以用到键盘监听事件,但是使用了JPanel之后添加了键盘监听事件,按相应的方向键飞机并没有反应.但是如果是为JFrame的内容面板加则会有反应. 为 ...

  4. ios --键盘监听JYKeyBoardListener

    没有前言,就是一个简单的键盘监听,自动调整输入框的位置不被键盘遮挡 .h // // JYKeyBoardListener.h // // Created by JianF.Sun on 17/9/2 ...

  5. Cocos Creator 键盘监听事件

    键盘事件键盘.设备重力传感器此类全局事件是通过函数 cc.systemEvent.on(type, callback, target) 注册的.cc.SystemEvent.EventType.KEY ...

  6. Android 软键盘监听事件

    Android软键盘的隐藏显示研究 Android是一个针对触摸屏专门设计的操作系统,当点击编辑框,系统自动为用户弹出软键盘,以便用户进行输入.     那么,弹出软键盘后必然会造成原有布局高度的减少 ...

  7. iOS键盘监听的通知

    #pragma mark - 键盘通知回调方法 //  监听键盘的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector: ...

  8. 键盘监听事件KeyListener

    说明:按下实体按键,相应虚拟按键变绿色,释放按键,变白色.   public class Demo extends JFrame { private List<JButton> list; ...

  9. vue 键盘监听事件

    <template> <div class="hello"> <input v-on:keyup.enter="submit" t ...

随机推荐

  1. 用eval转化对象

    var str = '{"name": "tom","age": 12,"sex": "man"}' ...

  2. 正式进入搭建OpenStack

    部署mariadb数据库 控制节点: yum install mariadb mariadb-server python2-PyMySQL -y 编辑: /etc/my.cnf.d/openstack ...

  3. RPC-Thrift(四)

    Client Thrift客户端有两种:同步客户端和异步客户端. 同步客户端 同步客户端比较简单,以RPC-Thrift(一)中的的例子为基础进行研究源码,先看一下类图. TServiceClient ...

  4. bzoj 2190 线性生成欧拉函数表

    首先我们知道,正方形内个是对称的,关于y=x对称,所以只需要算出来一半的人数 然后乘2+1就行了,+1是(1,1)这个点 开始我先想的递推 那么我们对于一半的三角形,一列一列的看,假设已经求好了第I- ...

  5. 160多条Windows 7 “运行”命令

    160多条Windows 7 “运行”命令: 删除或更改应用程序 = control appwiz.cpl 添加设备 = devicepairingwizard  蓝牙文件传输 = fsquirt  ...

  6. vue + webpack + gulp 简单环境 搭建

    一.物料准备 废话不多说,直接上 package.json { "name": "vwp", "version": "1.0.0& ...

  7. 2.kafka单节点broker的安装与启动

    下载kafka,http://kafka.apache.org/downloads kafka下面的文件结构如下: 进入bin目录,启动kafka之前要先启动zookeeper ./zookeeper ...

  8. PHP定时执行

    参考一 test.bat D:\myamp\php\php.exe -q D:\myamp\Apache\htdocs\dingshi.php dingshi.php <?php ignore_ ...

  9. 发布message给其他包使用

    https://answers.ros.org/question/65716/which-is-the-correct-way-to-install-header-files-in-catkin-pa ...

  10. Linux API的fork()测试

    现在到docker的实施阶段, 其底层的namespace,cgroup应该深入了解了. 其调用的API也慢慢熟悉起来吧. #include <unistd.h> #include < ...