[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";

// 键盘的bounds
     UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";

// 键盘的起始center
     UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}";

// 键盘的结束center
     UIKeyboardCenterEndUserInfoKey = "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

有错误还忘您指出,如果这篇文章帮助到您了,或者您有什么建议和补充,都可以留言告诉我哦!

 
时间: 2024-08-06 03:39:34

[iOS]利用通知实现监听系统键盘的相关文章

监听系统键盘的方法

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyBoardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil]; //监听通知的方法 -(void)KeyBoardWillChange:(NSNotification *)notification{ CGFloat height = [[[notification

iOS 通讯录编程,监听系统通讯录变更

监听通讯录变更 客户端代码需要这么实现: /* 移除注册函数 */ -(void)dealloc{ ABAddressBookUnregisterExternalChangeCallback(_addressBook, ContactsChangeCallback, nil); } /* 注册回调函数 */ - (id)init { self = [super init]; [self addressBookHandle]; ABAddressBookRegisterExternalChange

ios 监听系统键盘的出现和消失

//注册键盘出现的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil]; //注册键盘消失的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWi

Android App监听软键盘按键的三种方式(转)

最近有类似需求,在csdn上刚好发现,粘贴过来,以防止忘记喽 前言: 我们在android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的“GO”按键加载url页面:在点击搜索框的时候,点击右下角的search符号键可以进行搜索:或者在全部数据输入完毕后,点击右下角的"done"就马上进行下一步操作. 效果图: function 1: 重写Activity的dispatchKeyEvent(KeyEvent event)方法,在其中

键盘-App监听软键盘按键的三种方式

前言: 我们在android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的“GO”按键加载url页面:在点击搜索框的时候,点击右下角的search符号键可以进行搜索:或者在全部数据输入完毕后,点击右下角的"done"就马上进行下一步操作. 效果图: function 1: 重写Activity的dispatchKeyEvent(KeyEvent event)方法,在其中监听KeyEventKey.KEYCODE_ENTER键(右下

Android App监听软键盘按键的三种方式

前言: 我们在android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的"GO"按键加载url页面:在点击搜索框的时候,点击右下角的search符号键可以进行搜索:或者在全部数据输入完毕后,点击右下角的"done"就马上进行下一步操作. 效果图: function 1: 重写Activity的dispatchKeyEvent(KeyEvent event)方法,在其中监听KeyEventKey.KEYCODE

&lt;Android&gt;监听软键盘打开收起事件(软键盘自带收起按钮)

最近在公司开发cocos2dx上的android输入框控件,遇到软键盘的事件监听,通常软键盘的收起方式大致3种: 1.点击软键盘右下角的Return按钮(系统收起) 2.输入框焦点时按返回按钮(系统收起) 3.点击软键盘和输入框的外部(自发收起) 4.点击软键盘自带的收起按钮(软键盘收起) 前三种事件可以监听,方式都比较简单 1.点击软键盘右下角的Return按钮 给输入框设置监听 editText.setOnEditorActionListener(new OnEditorActionList

Android Notification之监听系统清除通知栏方法

编写了一个Notification通知工具类,里面含有监听系统清除通知栏方法,焦点在加粗斜体部分: public class Notifier { private static final String TAG = Notifier.class.getSimpleName(); private static Notifier instance; private Context mContext; private static final int NOTIFICATION_ID_1 = 0; pr

Android 监听 Android中监听系统网络连接打开或者关闭的实现代码

本篇文章对Android中监听系统网络连接打开或者关闭的实现用实例进行了介绍.需要的朋友参考下 很简单,所以直接看代码 复制代码 代码如下: package xxx; import android.content.BroadcastReceiver;  import android.content.Context;  import android.content.Intent;  import android.net.ConnectivityManager;  import android.ne