没有前言,就是一个简单的键盘监听,自动调整输入框的位置不被键盘遮挡


.h

//
// JYKeyBoardListener.h
//
// Created by JianF.Sun on 17/9/26.
// Copyright © 2017年 sjf. All rights reserved.
//
/*
功能:
1、输入框被键盘遮挡时,整个view上移(此时输入框在键盘上方)
2、键盘弹出时,添加一个按钮负责隐藏键盘
3、禁止某控制器使用JYKeyBoardListener
使用:
1、引入JYKeyBoardListener.h
2、程序启动时:[JYKeyBoardListener useJYKeyboardListener];
3、某些控制器不想使用时:[JYKeyBoardListener unUsedIn:vc];
处理:
1、监听键盘 显示,隐藏,退到后台,进入前台;
2、获取当前顶层控制器;
3、获取当前编辑的输入框在self.view中的frame;
4、键盘显示隐藏时动画;
5、隐藏键盘的按钮添加
6、处理push、present时键盘的显示隐藏问题(切记push之前,代码endEditing,因为push操作系统不会自动退出键盘)
7、处理self.view比屏幕小的问题(即self.view没有伸缩到导航栏底部)
8、处理输入框显示在屏幕中不完整的情况 */
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface JYKeyBoardListener : NSObject
//程序启动时调用
+ (void)useJYKeyboardListener;
//在viewcontroller中禁止使用JYKeyBoardListener
+ (void)unUsedIn:(UIViewController*)viewController;
@end

.m

//
// JYKeyBoardListener.m
//
// Created by JianF.Sun on 17/9/26.
// Copyright © 2017年 sjf. All rights reserved.
// #import "JYKeyBoardListener.h" #define KBL_Screen_Height [UIScreen mainScreen].bounds.size.height NSString * const JYKeyboard_Unused_Key= @"JYKeyboard_Unused_Key"; @interface JYKeyBoardListener ()
@property (nonatomic,strong) UIView *inputView;
@property (nonatomic,strong) UIButton *resignBtn;
@property (nonatomic,strong) UIView *lastView;//记录之前一个需要处理的view,解决push,present操作时,键盘的隐藏问题
//@property (nonatomic,strong) UIViewController *unUsedVC; @end
@implementation JYKeyBoardListener #pragma mark - 接口方法,直接调用即可
+ (void)useJYKeyboardListener{ [JYKeyBoardListener shareJYKeyBoardListener];
}
+ (void)unUsedIn:(UIViewController*)viewController{ JYKeyBoardListener *manager = [JYKeyBoardListener shareJYKeyBoardListener];
[[NSUserDefaults standardUserDefaults] setValue:[manager getMemory:viewController] forKey:JYKeyboard_Unused_Key];
} #pragma mark - 单例 + (instancetype)shareJYKeyBoardListener { static JYKeyBoardListener *jyKeyBoardListener = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
jyKeyBoardListener = [[JYKeyBoardListener alloc] init];
// 监听键盘 [[NSNotificationCenter defaultCenter] addObserver:jyKeyBoardListener selector:@selector(keyboardWillShowAction:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:jyKeyBoardListener selector:@selector(keyboardWillHideAction:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:jyKeyBoardListener selector:@selector(enterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:jyKeyBoardListener selector:@selector(enterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}); return jyKeyBoardListener;
} #pragma mark - 键盘显示和隐藏的监听方法
/**
* 键盘即将弹出
*/
- (void)keyboardWillShowAction:(NSNotification *)note{ if ([self currentMemory]!=nil&&[[self currentMemory] isEqualToString:[self getMemory:[self topViewController]]]) {
[self addResignBtn:[self topViewController].view];
return;
}else{
[[NSUserDefaults standardUserDefaults] setValue:nil forKey:JYKeyboard_Unused_Key];
} //结束时键盘的frame
CGRect endF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//键盘高度
CGFloat keyboardH = endF.size.height;
//键盘弹出需要的时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//当前的view
UIView *firstRspView = [self findFirstResponsderView]; if (firstRspView==nil) {
// NSLog(@"为空");
return ;
}
//找到输入框
[self findSubView:firstRspView]; //获取输入框相对于当前展示的最底层的那个view的frame
CGRect inputFrame = [self getAbsoluteFrame:self.inputView];
//
CGFloat difH = CGRectGetMaxY(inputFrame)-(KBL_Screen_Height-keyboardH);
// 2.动画
[UIView animateWithDuration:duration animations:^{ if (difH>) { firstRspView.transform = CGAffineTransformMakeTranslation(,-difH); }else{
firstRspView.transform = CGAffineTransformIdentity; }
_lastView = firstRspView;
}completion:^(BOOL finished) {
[self addResignBtn:firstRspView]; } ];
}
/**
* 键盘即将隐藏
*/
- (void)keyboardWillHideAction:(NSNotification *)note{ //判断当前控制器是否被禁用
if ([self currentMemory]!=nil&&[[self currentMemory] isEqualToString:[self getMemory:[self topViewController]]]) { return;
}else{
[[NSUserDefaults standardUserDefaults] setValue:nil forKey:JYKeyboard_Unused_Key];
}
// 1.键盘弹出需要的时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIView *firstRspView = [self findFirstResponsderView]; if(firstRspView==nil){
return;
}
if (_lastView&&_lastView!=firstRspView) {//处理跳转时恢复之前的,push之前必须 _lastView.transform = CGAffineTransformIdentity;
_lastView = nil;
return;
}
// 2.动画
[UIView animateWithDuration:duration animations:^{
firstRspView.transform = CGAffineTransformIdentity; }completion:^(BOOL finished) { }]; } #pragma mark - 前台后台切换处理
- (void)enterBackground:(NSNotification*)note{
[[self findFirstResponsderView] endEditing:YES];
}
- (void)enterForeground:(NSNotification*)note{ }
#pragma mark - 隐藏键盘按钮
- (void)addResignBtn:(UIView*)firstRspView{
if (self.resignBtn) {
[self.resignBtn removeFromSuperview];
}
self.resignBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.resignBtn.backgroundColor = [UIColor clearColor];
[self.resignBtn addTarget:self action:@selector(hideKeyboard) forControlEvents:UIControlEventTouchUpInside];
self.resignBtn.frame = firstRspView.bounds;
[firstRspView addSubview:self.resignBtn];
[firstRspView sendSubviewToBack:self.resignBtn];
}
- (void)hideKeyboard{
[[self findFirstResponsderView] endEditing:YES];
dispatch_async(dispatch_get_main_queue(), ^{
[self.resignBtn removeFromSuperview]; });
}
#pragma mark - 其他辅助性方法
/**
查找根控制器的view @return return FirstResponsderView
*/
- (UIView*)findFirstResponsderView{ UIViewController *returnVC; //查找当前的根控制器
returnVC = [self topViewController];
if (returnVC) {
return returnVC.view;
}else{
return nil;
}
}
// 获取当前屏幕显示的ViewController
- (UIViewController *)topViewController {
UIViewController *resultVC;
resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
while (resultVC.presentedViewController) {
resultVC = [self _topViewController:resultVC.presentedViewController];
}
return resultVC;
} - (UIViewController *)_topViewController:(UIViewController *)vc {
if ([vc isKindOfClass:[UINavigationController class]]) {
return [self _topViewController:[(UINavigationController *)vc topViewController]];
} else if ([vc isKindOfClass:[UITabBarController class]]) {
return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
} else {
return vc;
}
return nil;
} /*
获取输入框的相对于根控制器view的frame
*/
- (CGRect)getAbsoluteFrame:(UIView*)view{ UIView *mainView = [self findFirstResponsderView];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
CGRect rect=[view convertRect: view.bounds toView:mainView];
//处理导航栏问题
if (CGRectGetHeight(mainView.bounds)<CGRectGetHeight(window.bounds)) {
rect.origin.y += CGRectGetHeight(window.bounds)-CGRectGetHeight(mainView.bounds);
}
//滚动视图返回的frame去掉scrollView.contentOffset.y
if ([mainView isKindOfClass:[UIScrollView class]]||[mainView isKindOfClass:[UITableView class]]||[mainView isKindOfClass:[UIWebView class]]) {
UIScrollView *scrollView = (UIScrollView*)mainView;
rect.origin.y -=scrollView.contentOffset.y;
if (CGRectGetMaxY(rect)>KBL_Screen_Height) {//输入框没有完全显示出来 //去掉屏幕外的高度值
rect.origin.y-=CGRectGetMaxY(rect)-KBL_Screen_Height;
}
} return rect;
} /*
递归法
找到输入框
*/
- (void)findSubView:(UIView*)view{ for (UIView *subView in view.subviews){ if ([subView isFirstResponder]) {
// NSLog(@"找到输入框");
self.inputView = subView;
}else{
[self findSubView:subView];
}
}
} - (NSString*)getMemory:(UIViewController*)vc{
//控制器名称+内容的内存地址+view的内容地址
return [NSString stringWithFormat:@"jykeyboard%@%p%p",NSStringFromClass([vc class]),vc,vc.view];
}
- (NSString*)currentMemory{
return [[NSUserDefaults standardUserDefaults] valueForKey:JYKeyboard_Unused_Key];
}
@end

ios --键盘监听JYKeyBoardListener的更多相关文章

  1. iOS键盘监听事件

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

  2. iOS键盘监听的通知

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

  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. Java生成静态HTML文件

    private static final String FILEPATH = "/opt/nginx/html/banner/"; private static final Str ...

  2. Papers | 图像/视频增强 + 深度学习

    目录 I. ARCNN 1. Motivation 2. Contribution 3. Artifacts Reduction Convolutional Neural Networks (ARCN ...

  3. Singleton Summary

    Java Singleton: Singleton pattern restricts the instantiation of a class and ensures that only one i ...

  4. Android-Java-子类实例化过程(内存图)

    案例一: package android.java.oop15; // 描述Person对象 class Person { // 构造方法就算不写 默认有一个隐式的无参构造方法:public Pers ...

  5. 在Git中设置自己的姓名

    在Git中,自己的姓名与每一个commit提交绑定在一起.如果你在使用Azure DevOps Server中的Git Repo时,一定要注意commit中的提交者与服务器上的推送者,是两个概念. 在 ...

  6. Scala微服务架构 一

    因为公司的一个项目需求变动会非常频繁,同时改动在可控范围内,加上产品同学喜欢一些超前思维,我们的CTO决定提前开启八门,使用微服务架构. 划重点 微服务架构主要特点: ==独立组件(自主开发升级)== ...

  7. spring 原理1:java 模拟springIOC容器

    本篇博客主要是使用java代码模拟spring的IOC容器,实现依赖注入:当然只是模拟spring容器中简单的一点实现原理而已,加深一些自己对spring框架的底层原理的理解: 使用的技术:dom4j ...

  8. Ajax 的学习

    (一)基础知识和新的概念      1,AJAX 就是浏览器提供的一套 API,可以通过 JavaScript 调用,从而实现通过代码控制请求与响应.实现 网络编程.   2,AJAX(Asynchr ...

  9. Spring Boot定制启动图案

    启动图案 Spring Boot在启动的时候会显示一个默认的Spring的图案,对应的类为SpringBootBanner. . ____ _ __ _ _ /\\ / ___'_ __ _ _(_) ...

  10. vue环境安装

    node.js安装 https://nodejs.org/en/ cnpm安装 npm install -g cnpm --registry=https://registry.npm.taobao.o ...