iOS监听键盘和输入法切换事件
2023-09-11 14:22:56 时间
本单元教程基于 ”程序员长弓——张燕广“ 的 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
运行结果
相关文章
- iOS配置支付宝
- Unity手游iOS内存分析和测试
- iOS:图像和点击事件
- iOS:NAV+TABLE结合
- HTML5-IOS WEB APP应用程序(IOS META)
- iOS之事件穿透
- 《iOS 9 开发指南》——第1章,第1.1节IOS开发入门
- 《iOS创意程序设计家》——第6.4节事件检测
- iOS开发UIEvent事件简介
- 检测 iOS 系统网络权限被关闭
- Android/IOS 微信及浏览器(h5)中唤起本地APP,唤起浮层,然后用外部浏览器打开唤起某本地APP
- iOS工程师2021年应该学习一下Vapor
- App Launch Sequence on iOS 事件与UI
- iOS视图更新与事件循环
- iOS 10 的一个重要更新-开发 iMessage 的第三方插件
- iOS之事件穿透
- iOS archive(归档)
- 《iOS应用软件设计之道》—— 3.4 小结
- IOS 被拒 关于 iPhone running iOS 10.3.1 on Wi-Fi connected to an IPv6 network.
- IOS_ios逆向工程-静态分析
- iOS为移动中的UIView(UIButton )添加点击事件
- iOS上如何让按钮(UIbutton)文本左对齐展示
- iOS 依据文本内容为TextView动态定义高度
- iOS开发- runtime基本用法解析和用runtime给键盘添加工具栏和按钮响应事件
- iOS - UIEvent事件及UIResponder响应者