[iOS]利用通知实现监听系统键盘
生活随笔
收集整理的這篇文章主要介紹了
[iOS]利用通知实现监听系统键盘
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//
// ViewController.m
// text
//
// Created by 李東旭 on 16/1/22.
// Copyright ? 2016年 李東旭. All rights reserved.
//#import <UIKit/UIKit.h>
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];UITextView *textV = [[UITextView alloc] initWithFrame:self.view.bounds];[self.view addSubview:textV];// 1. 添加通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardChange:) name:UIKeyboardWillShowNotification object:nil];// 鍵盤將要出現 UIKeyboardWillShowNotification// 鍵盤已經出現 UIKeyboardDidShowNotification// 鍵盤將要隱藏 UIKeyboardWillHideNotification// 鍵盤已經隱藏 UIKeyboardDidHideNotification}// 2. 鍵盤已經出現調用的觸發方法
- (void)keyBoardChange:(NSNotification *)noti
{NSLog(@"%@", noti);
// 打印noti/*0x7f84584d1000 {name = UIKeyboardDidShowNotification; userInfo = {
// 鍵盤動畫執行節奏UIKeyboardAnimationCurveUserInfoKey = 7;// 鍵盤動畫的時間UIKeyboardAnimationDurationUserInfoKey = "0.25";// 鍵盤的boundsUIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";// 鍵盤的起始centerUIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}";// 鍵盤的結束centerUIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";// 鍵盤開始動畫前的位置(也就是消失時)UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";// 鍵盤結束動畫后的位置(也就是彈出了)UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";UIKeyboardIsLocalUserInfoKey = 1;}}*/// 3. 那么如何獲取這個值呢NSDictionary *noKey = [noti userInfo];NSValue *value = noKey[@"UIKeyboardBoundsUserInfoKey"];// 注意,這個的value 是CGRect類型的(所以下面必須調用CGRectValue方法NSLog(@"%f", [value CGRectValue].size.height);}@end
轉載于:https://www.cnblogs.com/wangqi1221/p/5240217.html
總結
以上是生活随笔為你收集整理的[iOS]利用通知实现监听系统键盘的全部內容,希望文章能夠幫你解決所遇到的問題。