#pragma mark - 键盘通知回调方法

//  监听键盘的通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];

/*

UIKeyboardAnimationCurveUserInfoKey = 7;   键盘执行动画的节奏

UIKeyboardAnimationDurationUserInfoKey = "0.25"; 键盘弹出动画执行时间

UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}"; 键盘尺寸

UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}"; 键盘当前的位置

UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";  键盘改变后中心的位置

UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}"; 键盘当前的frame

UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}"; 键盘改变后的frame

*/

- (void)keyboardWillChangeFrameNotification:(NSNotification *) notification

{

//    NSLog(@"%@",notification.userInfo);

//  1.取出键盘当前的y坐标

CGRect beginFrame =  [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];

CGRect endFrame =  [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

//  2.中间的差值

CGFloat minus =  endFrame.origin.y - beginFrame.origin.y ;

//  3.根据差值改变控制器view的frame

CGRect viewFrame = self.view.frame;

viewFrame.origin.y += minus;

self.view.frame = viewFrame;

}

#pragma mark - tableView代理方法

/**

*  拖拽tableView方法

*/

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

//  退出键盘

[self.view endEditing:YES];

}

#pragma mark -dealloc方法写到最后

- (void)dealloc

{

//  移除通知

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

//随输入框的高度改变高度 >> 用于ScorllView中添加的View !!!

- (void)keyboardWillHiden:(NSNotification *)notification{

NSDictionary* info = [notification userInfo];

CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);

self.loginScorllView.contentInset = contentInsets;

self.loginScorllView.scrollIndicatorInsets = contentInsets;

CGRect aRect = self.view.frame;

aRect.size.height -= kbSize.height;

if (!CGRectContainsPoint(aRect, self.loginTitle.frame.origin) ) {

CGPoint scrollPoint = CGPointMake(0.0, self.loginTitle.frame.origin.y-kbSize.height);

[ self.loginScorllView setContentOffset:scrollPoint animated:YES];

}

}

//  通用的 >> 只需改 上面ty的值就行!!

-(void) viewDidAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShowHandler:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHideHandler:)
name:UIKeyboardWillHideNotification
object:nil];
}

-(void) viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}

- (void)keyboardWillShowHandler:(NSNotification *)notification
{
NSDictionary * userInfo = notification.userInfo;

CGRect frameEndUserInfo = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat cure = [userInfo[UIKeyboardAnimationCurveUserInfoKey] floatValue];

// CGRect toFrame = self.frame;

CGFloat ty = frameEndUserInfo.origin.y-frameEndUserInfo.origin.y+70;

if (self.view.y != ty) {
[UIView animateWithDuration:duration animations:^{
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:cure];

self.view.y = -ty;
// self.frame = toFrame;
}];
}
}

- (void)keyboardWillHideHandler:(NSNotification *)notification
{

NSDictionary * userInfo = notification.userInfo;

CGRect frameEndUserInfo = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat cure = [userInfo[UIKeyboardAnimationCurveUserInfoKey] floatValue];

// CGRect toFrame = self.frame;

CGFloat ty = frameEndUserInfo.origin.y -frameEndUserInfo.origin.y-64;

if (self.view.y != ty) {
[UIView animateWithDuration:duration animations:^{
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:cure];

self.view.y = -ty;
// self.frame = toFrame;
}];
}
}

iOS键盘监听的通知的更多相关文章

  1. ios --键盘监听JYKeyBoardListener

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

  2. iOS键盘监听事件

    1.注册键盘通知事件 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; // 键盘将出现事件监听 [center ...

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

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

  4. 使用HTML+CSS,jQuery编写的简易计算器后续(添加了键盘监听)

    之前发布了一款简易的计算器,今天做了一下修改,添加了键盘监听事件,不用再用鼠标点点点啦 JS代码: var yunSuan = 0;// 运算符号,0-无运算;1-加法;2-减法;3-乘法;4-除法 ...

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

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

  6. java swing button和键盘监听冲突问题

    原因: 点击button会让jframe失去焦点,然后键盘监听不起作用 解决: 让jframe重新获取焦点就行了 jf.setFocusable(true); // JFrame jf = new J ...

  7. JPanel添加键盘监听事件

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

  8. C#全局键盘监听(Hook)

    一.为什么需要全局键盘监听? 在某些情况下应用程序需要实现快捷键执行特定功能,例如大家熟知的QQ截图功能Ctrl+Alt+A快捷键,只要QQ程序在运行(无论是拥有焦点还是处于后台运行状态),都可以按下 ...

  9. 【转】【C#】全局键盘监听

    using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServi ...

随机推荐

  1. POJ 1061 青蛙的约会

                            青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 82859   A ...

  2. TP3.1 中URL和APP区别

    1.__URL__指当前模块地址,就是当前的action的地址.(每个__action都是一个模块)    eg:当前打开config.html,那么config.html里边的__URL__/sav ...

  3. h5交互元素details标签

    details是h5新增的交互元素,details与 summary 标签配合使用可以为 details 定义标题.默认情况下,不显示 details 标记中的内容.当用户点击标题时,会显示出 det ...

  4. oracle数据表创建分区与查询

    场景: 遇到1亿数据量的数据需要根据用户名做些数据统计分析,想直接做些聚合计算基本没可能,于是打算先根据日期按照年月创建分区,然后对各个分区分别进行统计,最后汇总结果. 有两种方法,分别是手工设置分区 ...

  5. keepalived安装

    两台虚拟机 两台配置操作一样 环境配置 [root@lb01 /]# yum -y install openssl openssl-devel [root@lb01 /]# yum -y instal ...

  6. java web 相对路径中已/开头和不已/开头的区别

    通俗的讲,有/会从跟目录开始算,没有会从当前目录开始算 1.前台页面 ​页面中向服务器页面请求静态资源且没有指定<base href="<%=basePath%>" ...

  7. android定位

    先说说手机定位的方式 1,GPS 绝大部分手机都有GPS模块,这种方式准确度是最高的,但是缺点也很明显,1,耗电高:2,绝大部分用户默认不开启GPS模块.3,从GPS模块启动到获取第一次定位数据,可能 ...

  8. BZOJ1901——Zju2112 Dynamic Rankings

    1.题目大意:区间第k小,有单点修改 2.分析:这个是树状数组套线段树,也是主席树....为什么主席树这么多QAQ 就是树套树的那种插入什么的,注意啊,一定要动态开内存..不然会爆.. 然后算答案有两 ...

  9. windows2008安装IIS

    一个朋友说不会使用2008搭建IIS.刚好我之后也要学习如何绕过waf需要安装一些环境.就帮个忙了,写这篇文章吧. 进入服务器管理. 添加角色 选择:WEB服务器(IIS)后会提示添加角色向导.那么久 ...

  10. ubuntu: NO_PUBKEY 8D5A09DC9B929006

    最近使用ubuntu16.04时,运行 sudo apt-get update 时出现如下错误: W: GPG error: http://archive.ubuntukylin.com:10006/ ...