iOS监听键盘和输入法切换事件
来源:互联网 发布:淘宝一个好评多少分 编辑:程序博客网 时间:2024/06/01 15:38
本单元教程基于 ”程序员长弓——张燕广“ 的 iphone开发教程,使用最新版的 ios9 和 xcode7 作为开发工具编译运行。链接地址 iPhone开发【二十八】监听键盘状态、输入法状态
//// ViewController.m// KeyboardDemo//// Created by 555chy on 6/11/16.// Copyright © 2016 555chy. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //键盘完全显示 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; //键盘完全隐藏 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; //输入法切换 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputModeDidChange:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];}- (void) keyboardDidShow:(NSNotification*) notification { NSLog(@"keyboardDidShow"); //UIKeyboardFrameBeginUserInfoKey 表示动画完成之前的键盘框架(这时候键盘的坐标在屏幕下面,还未显示出来) //UIKeyboardFrameEndUserInfoKey 表示动画完成之后的键盘框架(这时候键盘的坐标在屏幕下方) NSValue *beginValue = [[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey]; CGRect beginKeyboardRect = [beginValue CGRectValue]; NSLog(@"beginKeyboardRect %.2f %.2f %.2f %.2f", beginKeyboardRect.origin.x, beginKeyboardRect.origin.y, beginKeyboardRect.size .width, beginKeyboardRect.size.height); CGRect beginKeyboardFrame = [self.view convertRect:beginKeyboardRect fromView:[[UIApplication sharedApplication] keyWindow]]; NSLog(@"beginKeyboardFrame %.2f %.2f %.2f %.2f", beginKeyboardFrame.origin.x, beginKeyboardFrame.origin.y, beginKeyboardFrame.size .width, beginKeyboardFrame.size.height); //通过比对可疑发现键盘的坐标和整个keywindow是一致的,所以可以不用坐标转换其实 NSValue *endValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; CGRect endKeyboardRect = [endValue CGRectValue]; NSLog(@"endKeyboardRect %.2f %.2f %.2f %.2f", endKeyboardRect.origin.x, endKeyboardRect.origin.y, endKeyboardRect.size .width, endKeyboardRect.size.height); CGRect endKeyboardFrame = [self.view convertRect:endKeyboardRect fromView:[[UIApplication sharedApplication] keyWindow]]; NSLog(@"endKeyboardFrame %.2f %.2f %.2f %.2f", endKeyboardFrame.origin.x, endKeyboardFrame.origin.y, endKeyboardFrame.size .width, endKeyboardFrame.size.height); }- (void) keyboardDidHide:(NSNotification*) notification { NSLog(@"keyboardDidHide");}- (void) inputModeDidChange:(NSNotification*) notification { NSLog(@"inputModeDidChange"); //currentInputMode is deprecated first in iOS7,因为它一次仅能输出一种(例如en-US) NSLog(@"[UITextInputMode currentInputMode].primaryLanguage %@", [UITextInputMode currentInputMode].primaryLanguage); for (UITextInputMode *mode in [UITextInputMode activeInputModes]) { //会输出 en-US、emoji等 NSLog(@"mode.primaryLanguage %@", mode.primaryLanguage); } }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
运行结果
1 0
- iOS监听键盘和输入法切换事件
- ios 动态监听键盘输入法和高度
- ios 动态监听键盘输入法和高度
- 监听输入法键盘的事件
- iOS swift3.0 监听textField和textView的键盘事件
- 事件 鼠标监听和键盘监听
- iOS 监听键盘高度,监听键盘出现 收回事件
- Android监听系统输入法键盘弹出显示与隐藏事件
- pyhook监听鼠标事件和键盘事件
- iOS study Day20-监听键盘回车事件
- iOS监听键盘的删除按键事件
- iOS监听键盘的删除按键事件
- ios-监听键盘的弹起事件
- iOS开发中监听键盘状态、输入法状态
- 键盘无法切换输入法
- iOS监听tableView组头切换事件
- 监听iPad键盘显示和隐藏事件
- 用Python监听鼠标和键盘事件
- CSDN第一篇:处女座
- Word Search
- Android Simple And Powerful Cache Manager
- select函数的使用举例
- Linked List Cycle
- iOS监听键盘和输入法切换事件
- 日期的处理 - 高斯日记 - 端午节 - 计算日期后续
- Android内存泄露检测工具---LeakCanary的前世今生
- Decode Ways
- Best Time to Buy and Sell Stocks
- MessageQueue&Message Pool—消息的读写、删除和回收
- 日期联动框
- C#中的深复制和浅复制(在C#中克隆对象)
- 《笑话大全》APP代码详解3