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. 在Maven中怎么配置外部Jar

    转摘自:http://liugang594.iteye.com/blog/1677712 假设我们有一个Maven的project,其中有些Jar包不是来自Maven库的,是存在本地的某些Jar文件, ...

  2. 慕课网javascript 进阶篇 第九章 编程练习

    把平常撸的码来博客上再撸一遍既可以加深理解,又可以理清思维.还是很纯很纯的小白,各位看官老爷们,不要嫌弃.最近都是晚睡,昨晚也不例外,两点多睡的.故,八点起来的人不是很舒服,脑袋有点晕呼呼,鉴于昨晚看 ...

  3. Web项目中加载Spring配置的常用方法

    1.web.xml中添加配置 <web-app>      <context-param>         <param-name>contextConfigLoc ...

  4. 复选框 checkbox 选中事件

    项目中用的jquery-1.11 今天需要检测一个checkbox的选中状态,想当然的用 .attr("checked") ,结果发现,无论是否选中,这个值都是 undefined ...

  5. 动态规划:划分DP

    划分型动态规划之数的划分 先贴上2014年10月31日的那份代码,甚至怀念当时在机房YY这道题的场面 高中最快乐的时候.. #include<iostream> using namespa ...

  6. linux认证上网

    公司上网需要认证,linux命令行模式:curl -d "opr=pwdLogin&userName=用户名&pwd=密码&rememberPwd=1" h ...

  7. 【Foreign】字符串匹配 [KMP]

    字符串匹配 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 3 3 6 3 1 2 1 2 ...

  8. php发送请求

    $opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=>10, ));$context = strea ...

  9. bzoj 3196二逼平衡树 线段树套平衡树

    比较裸的树套树,对于区间K值bz上有一道裸题,详见题解http://www.cnblogs.com/BLADEVIL/p/3455336.html(其实题解也不是很详细) //By BLADEVIL ...

  10. JavaSript中数组方法是否对原数组产生影响

    JavaScript中数组方法有很多.某次面试被问到,concat()方法会对影响到原数组吗.当时记得不牢,犹豫地说"会吧...".于是决定总结一下哪些数组方法会对原数组产生影响. ...