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
原创粉丝点击