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 ...
随机推荐
- The 'brew link' step did not complete successfully
在mac 上更新node时遇到了一系列的问题: 卸载node重新安装之后提示: The 'brew link' step did not complete successfully 其实这里已经给出了 ...
- 有关getClassLoader().getResourceAsStream(fileName)、class.getResourceAsStream(fileName)和().getContextClassLoader().getResourceAsStream(fileName)的区别
一:前言 在自己获取属性时,碰见了XX.class.getResourceAsStream(fileName),自己对这个其实不是很理解,上网查了下资料,又看到了上述的几个,所以就研究了下. 二:内容 ...
- Spring - IoC(7): 延迟实例化
默认情况下,Spring IoC 容器启动后,在初始化过程中,会以单例模式创建并配置所有使用 singleton 定义的 Bean 的实例.通常情况下,提前实例化 Bean 是可取的,因为这样在配置中 ...
- 【Atcoder】ARC 080 E - Young Maids
[算法]数学+堆 [题意]给定n个数的排列,每次操作可以取两个数按序排在新序列的头部,求最小字典序. [题解] 转化为每次找字典序最小的两个数按序排在尾部,则p1和p2的每次选择都必须满足:p1在当前 ...
- 我在开发中所遇到的iOS7新特性以及iOS7与iOS6的适配问题总结
⓵UIImageView 1. // iOS7添加的对图像颜色处理的功能,过滤颜色的功能 2. _imageView.tintColor = [UIColor blueColor]; 3. //重 ...
- [Leetcode Week3]Clone Graph
Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...
- JQ子页面对父页面的元素进行操作
需要加上parent.document,才能找到父页面的元素 如: $("#tabs", parent.document).click();
- Spring MVC基础篇4
Spring MVC操作原生Servlet 对象 Spring MVC 可以操作原生的Servlet API,如下的这些原生API,可以各自 自由混合使用,也可以和其他非原生 参数组合使用 实例代码: ...
- Selenium2+python自动化35-获取元素属性【转载】
前言 通常在做断言之前,都要先获取界面上元素的属性,然后与期望结果对比.本篇介绍几种常见的获取元素属性方法. 一.获取页面title 1.有很多小伙伴都不知道title长在哪里,看下图左上角. 2.获 ...
- 开发者应该了解的API技术清单!
英文原文:API-Driven Development 作为一名开发者,诚然编写代码如同作家提笔挥毫,非常有成就感与乐趣,但同时我也觉得删除代码是件不相伯仲的美事.为什么呢?因为在进行删除工作时,意味 ...