监听键盘高度变换

//
//  JLKeyboardListener.h
//  Test
//
//  Created by Jack on 2017/4/11.
//  Copyright ? 2017年 buakaw. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol JLKeyboardListenerDelegate <NSObject>

@required
- (void)keyboardWillShowHeight:(CGFloat)height withDuration:(NSTimeInterval)duration;

- (void)keyboardWillHideHeight:(CGFloat)height withDuration:(NSTimeInterval)duration;

/**
 键盘高度发生改变

 @param height 新的键盘高度
 @param heightOffset 发生改变偏移量,大于0表示高度增加
 @param duration 动画持续时间
 */
- (void)keyboardWillChangeHeight:(CGFloat)height offset:(CGFloat)heightOffset withDuration:(NSTimeInterval)duration;

@end

@interface JLKeyboardListener : NSObject

@property (nonatomic,weak) id<JLKeyboardListenerDelegate> delegate;

@property (nonatomic,assign,readonly) CGFloat currentKeyboardHeight;

@end



//
//  JLKeyboardListener.m
//  Test
//
//  Created by Jack on 2017/4/11.
//  Copyright ? 2017年 buakaw. All rights reserved.
//

#import "JLKeyboardListener.h"

@interface JLKeyboardListener ()

@property (nonatomic,assign) CGFloat keyboardHeight;
@property (nonatomic,assign) NSTimeInterval keyboardAnimationDuration;
@property (nonatomic,assign) BOOL showsKeyboard;

@end

@implementation JLKeyboardListener

- (instancetype)init {
    if (self = [super init]) {
        [self becomeKeyboardObserver];
        self.showsKeyboard = NO;
    }
    return self;
}

- (void)dealloc {
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter removeObserver:self];
}

- (void)becomeKeyboardObserver {
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    // 键盘通知
    [notificationCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [notificationCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [notificationCenter addObserver:self selector:@selector(keyboardChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

}

#pragma mark - keyboard Notification

/**键盘隐藏*/
- (void)keyboardWillHide:(NSNotification *)notification {

    if (self.showsKeyboard) {
        self.showsKeyboard = NO;

        if (self.delegate && [self.delegate respondsToSelector:@selector(keyboardWillHideHeight:withDuration:)]) {
            [self.delegate keyboardWillHideHeight:self.keyboardHeight withDuration:self.keyboardAnimationDuration];
        }
    }
}

/**键盘显示*/
- (void)keyboardWillShow:(NSNotification *)notification {

    NSTimeInterval duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect begin = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGRect end = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    if (duration == 0) {
        duration = 0.25;
    }
    self.keyboardAnimationDuration = duration;
    if (!self.showsKeyboard) {
        // 第三方键盘三次回调,仅执行最后一次(仅以搜狗输入法作的测试)
        if (begin.size.height > 0 && begin.origin.y - end.origin.y > 0) {

            self.showsKeyboard = YES;
            self.keyboardHeight = end.size.height;
            if (self.delegate && [self.delegate respondsToSelector:@selector(keyboardWillShowHeight:withDuration:)]) {
                [self.delegate keyboardWillShowHeight:self.keyboardHeight withDuration:self.keyboardAnimationDuration];
            }

        }
    }

}

/**在使用过程中切换键盘*/
- (void)keyboardChangeFrame:(NSNotification *)notification {

    if (self.showsKeyboard) {
        CGRect begin = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
        CGRect end = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

        // 在ip6s中使用搜狗输入法和系统键盘切换时发现
        // sougou --> sys : 282 -> 258
        // sys --> sougou : 258 -> 0 -> 216 -> 282 也就是说系统键盘先下去隐藏,再弹出搜狗,在弹出搜狗的过程中在216处有回调

        CGFloat offset = end.size.height - begin.size.height;
        self.keyboardHeight = end.size.height;

        if (offset != 0) {
            NSTimeInterval changeFrameAnimationDuartion = 0.25;
            if (self.delegate && [self.delegate respondsToSelector:@selector(keyboardWillChangeHeight:offset:withDuration:)]) {
                [self.delegate keyboardWillChangeHeight:self.keyboardHeight offset:offset withDuration:changeFrameAnimationDuartion];
            }
        }

    }
}

#pragma mark - Getter

- (CGFloat)currentKeyboardHeight {
    return self.keyboardHeight;
}

@end

				
时间: 2024-10-29 19:06:32

监听键盘高度变换的相关文章

iOS 监听键盘高度,输入框上升

1 //设置输入框 ---<因为输入框用了get方法,所以第一次调用输入框要用self 调用>: 2 self.textlab.frame=CGRectMake(20, 420, 250, 30); 3 _textlab.layer.borderColor=[UIColor blueColor].CGColor; 4 _textlab.layer.borderWidth= 0.5f; 5 _textlab.backgroundColor=[UIColor colorWithRed:0.830

iOS开发之监听键盘高度的变化

最近做的项目中,有一个类似微博中的评论转发功能,屏幕底端有一个输入框用textView来做,当textView成为第一响应者的时候它的Y值随着键盘高度的改变而改变,保证textView紧贴着键盘,但又不会被键盘挡住. 下面是我实现的方法:(利用通知) // 键盘通知 // 键盘的frame发生改变时发出的通知(位置和尺寸) // UIKeyboardWillChangeFrameNotification // UIKeyboardDidChangeFrameNotification // 键盘显

iOS监听键盘高度的变化

最近做的项目中,有一个类似微博中的评论转发功能,屏幕底端有一个输入框用textView来做,当textView成为第一响应者的时候它的Y值随着键盘高度的改变而改变,保证textView紧贴着键盘,但又不会被键盘挡住. 下面是我实现的方法:(利用通知) // 键盘通知// 键盘的frame发生改变时发出的通知(位置和尺寸)// UIKeyboardWillChangeFrameNotification// UIKeyboardDidChangeFrameNotification// 键盘显示时发出

iOS——UIKeyboardWillShowNotification 监听键盘高度变化

- (void)viewDidLoad { [super viewDidLoad]; UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 50)]; textField.backgroundColor = [UIColor blueColor]; [self.view addSubview:textField]; [[NSNotificationCenter defaultCen

swift 监听键盘弹出的高度

// 监听键盘通知 NotificationCenter.default.addObserver(self, selector: #selector(ComposeViewController.keyboardWillChangeFrame(note:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil) //监听键盘的事件 func keyboardWillChangeFrame(note: Notificat

通过KVO来监听键盘弹出和弹回

在通知中心建立一个广播来监听键盘的弹出和弹回,在监听事件中加入触发事件的一些操作. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self sele

监听键盘事件

注册监听键盘事件: 1 // 键盘即将隐藏 2 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 3 4 // 键盘已经隐藏 5 [[NSNotificationCenter defaultCenter] addObserver:self selector:@sel

21-30(NSTimer定时器 Cell的重用原理 代理的使用场合 UITableViewCell结构 监听键盘的通知)

21.NSTimer定时器 22.tableView的基本用法 23.tableView的常用属性 24.Cell的重用原理: 25.UITableViewCell结构 26.使用xib封装一个view的步骤 27.代理的使用场合 28.使用delegate的步骤 29.通过代码自定义cell步骤 30.监听键盘的通知 { 细节决定成败, 这句话讲的太对了, 所以我们要注意每一个细节!今天还好注意了, 没犯错!嘿嘿! 今天心情特别好, 心情好! 啥都好! 给大家来个笑, 工作的同时,不要忘记笑容

2016-02-22 监听键盘 隐藏bar

- (BOOL)prefersStatusBarHidden{ return YES; }   //隐藏bar 2:让键盘消失 ////    [_lastField resignFirstResponder]; //    [self.view endEditing:YES];//gzz0223 // //    //gzz0223 键盘消失 //    NSArray *subviews = [self.view subviews]; //    for (id objInput in su