代码改变世界

KeyboardNotificationViewController

2018-11-22 16:11  法子  阅读(317)  评论(0编辑  收藏  举报

GitHub地址:https://github.com/liuyongfa/KeyboardNotificationViewController.git

继承KeyboardNotificationViewController,并且重写函数

- (UIView *)bottomViewVisible {
    return self.loginButton;
}

返回最底部不想被键盘覆盖的控件。就可以实现view跟随键盘移动。同时支持点击空白处收起键盘。

LYFKeyboardNotificationViewController.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface LYFKeyboardNotificationViewController : UIViewController
/**
 继承GMCKeyboardNotificationViewController的子类提供给父类
 
 @return  最底部希望不被键盘覆盖的控件
 */
- (nullable UIView *)bottomViewVisible;
@end
NS_ASSUME_NONNULL_END

LYFKeyboardNotificationViewController.m

#import "LYFKeyboardNotificationViewController.h"

static CGFloat VerticalOffset = 8.0;

@interface LYFKeyboardNotificationViewController ()

@end

@implementation LYFKeyboardNotificationViewController

//storyboard
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self addKeyboardNotification];
    }
    return self;
}
//非storyboard
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self  = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self addKeyboardNotification];
    }
    return  self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createGestureRecognizer];
}

- (UIView *)bottomViewVisible {
    return nil;
}

#pragma mark - KeyboardNotification
- (void)addKeyboardNotification {
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShowAnimated:(CGRect)keyboardFrame {
    UIView *v = [self bottomViewVisible];
    if (!v) {
        return;
    }
    CGRect buttonRect = [v.superview convertRect:v.frame toView:self.view];
    CGFloat height = self.view.frame.size.height - CGRectGetMaxY(buttonRect);
    CGFloat needH = MIN(0, height - keyboardFrame.size.height - VerticalOffset);
    self.view.transform = CGAffineTransformMakeTranslation(0, needH);
}

- (void)keyboardWillHideAnimated:(CGRect)keyboardFrame {
    self.view.transform = CGAffineTransformMakeTranslation(0, 0);
}

- (void)keyboardWillShowNotification:(NSNotification*)notification {
    [self handleNotification:notification willShow:YES];
}

- (void)keyboardWillHideNotification:(NSNotification*)notification {
    [self handleNotification:notification willShow:NO];
}

- (void)handleNotification:(NSNotification *)notification willShow:(BOOL)show {
    NSDictionary *userInfo = notification.userInfo;
    CGRect frame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    int curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] intValue];
    __weak LYFKeyboardNotificationViewController *weakSelf = self;
    [UIView animateWithDuration:duration delay:0 options:curve animations:^{
        show ? [weakSelf keyboardWillShowAnimated:frame] : [weakSelf keyboardWillHideAnimated:frame];
    } completion:^(BOOL finished) {
    }];
}

#pragma mark - TapGesture
-(void)createGestureRecognizer {
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnView)];
    [self.view addGestureRecognizer:tapGR];
}

-(void)tapOnView {
    [self.view endEditing:YES];
}

- (void)dealloc {
    [NSNotificationCenter.defaultCenter removeObserver:self];
}

@end