iOS监听键盘和输入法切换事件

本单元教程基于 ”程序员长弓——张燕广“ 的 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

运行结果





  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值