iOS键盘监听事件
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键盘监听事件的更多相关文章
- js 获取当前焦点所在的元素、给元素和input控件添加键盘监听事件、添加页面级的键盘监听事件
页面级的键盘监听事件 document.onkeydown = function (event) { var e = event || window.event || arguments.callee ...
- [置顶] java Gui 键盘监听事件
简单写一个java Gui键盘监听事件,实现的效果就是按下键盘控制台输出你按下的键.比如:按下A控制台就输出A 效果如图: 以下把实现的效果分为几个步骤: 1.新建一个窗体类继承窗体: 2.给这个窗体 ...
- JPanel添加键盘监听事件
因为在自己的游戏需求中谢了要用键盘控制飞机的移动,所以用到键盘监听事件,但是使用了JPanel之后添加了键盘监听事件,按相应的方向键飞机并没有反应.但是如果是为JFrame的内容面板加则会有反应. 为 ...
- ios --键盘监听JYKeyBoardListener
没有前言,就是一个简单的键盘监听,自动调整输入框的位置不被键盘遮挡 .h // // JYKeyBoardListener.h // // Created by JianF.Sun on 17/9/2 ...
- Cocos Creator 键盘监听事件
键盘事件键盘.设备重力传感器此类全局事件是通过函数 cc.systemEvent.on(type, callback, target) 注册的.cc.SystemEvent.EventType.KEY ...
- Android 软键盘监听事件
Android软键盘的隐藏显示研究 Android是一个针对触摸屏专门设计的操作系统,当点击编辑框,系统自动为用户弹出软键盘,以便用户进行输入. 那么,弹出软键盘后必然会造成原有布局高度的减少 ...
- iOS键盘监听的通知
#pragma mark - 键盘通知回调方法 // 监听键盘的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector: ...
- 键盘监听事件KeyListener
说明:按下实体按键,相应虚拟按键变绿色,释放按键,变白色. public class Demo extends JFrame { private List<JButton> list; ...
- vue 键盘监听事件
<template> <div class="hello"> <input v-on:keyup.enter="submit" t ...
随机推荐
- Struts2 利用拦截器 interceptor 控制登陆和访问权限
最近学习了Struts2的登录和权限控制用到的是拦截器,需要在struts.xml中配置,每个action都默认的继承defaultStack,如果你用了别的拦截器,还需要手动引入defaultSta ...
- C++ 智能指针的简单实现
智能指针的用处:在c++中,使用普通指针容易造成堆内存的泄露问题,即程序员会忘记释放,以及二次释放,程序发生异常时内存泄漏等问题,而使用智能指针可以更好的管理堆内存.注意,在这里智能指针是一个类而非真 ...
- python面向对象之继承与派生
一.继承 继承是一种创建新的类的方式,在python中,新建的类可以继承自一个或者多个父类,原始类称为基类或超类,新建的类称为派生类或子类. python中类的继承分为:单继承和多继承,如果是多继承的 ...
- 转:Android 的一些比较好的开源代码项目
转自:http://blog.csdn.net/zengyangtech/article/details/7019439 Android PDF 阅读器 http://sourceforge.net/ ...
- mysql五-1:单表查询
一 介绍 本节内容: 查询语法 关键字的执行优先级 简单查询 单条件查询:WHERE 分组查询:GROUP BY HAVING 查询排序:ORDER BY 限制查询的记录数:LIMIT 使用聚合函数查 ...
- Python学习笔记 使用数据库SQlite Mysql
SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用当中, 甚至在IOS和Android的APP中都可以集成 Python就内 ...
- 如何在Ubuntu 16.04安装的Git【转】
转自:https://www.howtoing.com/how-to-install-git-on-ubuntu-16-04/ 介绍 现代软件开发中不可或缺的工具是某种版本控制系统. 版本控制系统允许 ...
- kernel 中 sscanf和sprintf()函数使用说明【转】
转自:http://blog.csdn.net/tommy_wxie/article/details/8480695 在将各种类型的数据构造成字符串时,sprintf 的强大功能很少会让你失望.由于s ...
- nginx1.11.9 apt即源码编译各平台测试
测试系统:ubuntu16.04 server,debian8.7 netinstall,centos7 mini. 系统配置:使用virtualbox安装,内存1G,cpu单核,物理CPU i5- ...
- docker官方文档中的dns,link,expose,publish
link是过时的了,尽量不要用. dns内部集成,也可以用外部. expose只是用于记录,并不真的. publish是否起作用,也要看情况,是否被占用端口. -------------------- ...