键盘弹出事件

将键盘弹出事件抽离,可以控制键盘弹起时视图的控制,主要用于控制view、tableView、scrollView

- (instancetype)init {

    if (self = [super init]) {

        [self keyBoard];

    }

    return self;

}


+ (instancetype)keyBoardWithSuperView:(UIView *)superView scrollView:(UIScrollView *)scrollView delegate:(id)delegate {

    LJBKeyBoard *keyBoard = [[self alloc] init];

    keyBoard.delegate = delegate;

    [superView addSubview:keyBoard];

    keyBoard.superView = superView;

    keyBoard.scrollView = scrollView;

    

    return keyBoard;

}


- (void)keyBoard {

    //键盘弹出的通知。

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardWillShowNotification object:nil];

    //键盘隐藏的通知。

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHide:) name:UIKeyboardWillHideNotification object:nil];

}


#pragma mark - 获取键盘弹出通知

- (void)keyBoardShow:(NSNotification *)note {

    NSDictionary *dict = note.userInfo;

    CGRect endRect = [dict[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];

    CGFloat endKeyY = 0;

    UIView *view = self.superView;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {

        endKeyY = view.frame.size.height - endRect.size.width;

    } else {

        endKeyY = endRect.origin.y;

    }

    UIScrollView *scrollView = self.scrollView;

    CGPoint offSet = scrollView.contentOffset;

    CGFloat currTextMaxY = [self.delegate ljbKeyBoardGetMaxY];

    CGFloat Fheight = (scrollView.frame.origin.y + currTextMaxY-offSet.y);

    if (Fheight > endKeyY) {

        view.transform = CGAffineTransformMakeTranslation(0, endKeyY - Fheight);

    }

}


#pragma mark - 获取键盘隐藏通知

- (void)keyBoardHide:(NSNotification *)note {

    self.superView.transform = CGAffineTransformIdentity;

}

1.具体的使用步骤 viewDidLoad 中定义事件 如果为tableview或者scrollview上弹起时,scrollview:传递具体的参数scrollView:self.tableView、scrollView:self.scrollView

[LJBKeyBoard keyBoardWithSuperView:self.view scrollView:nil delegate:self];

2.实现代理返回具体的位置

#pragma mark - 键盘代理事件(必须实现以下代理)

- (CGFloat)ljbKeyBoardGetMaxY {

    UITextField *textField = (UITextField *)[self.view viewWithTag:self.selTag];

    return CGRectGetMaxY(textField.frame);

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值