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

運行結果






注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com