zoukankan      html  css  js  c++  java
  • 通过NSNotification来监听键盘弹出和弹回

    在通知中心建立一个广播来监听键盘的弹出和弹回,在监听事件中加入触发事件的一些操作。

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

    监听键盘的一些通知:

        // 键盘的frame发生改变时发出的通知(位置和尺寸)
        //    UIKeyboardWillChangeFrameNotification
        //    UIKeyboardDidChangeFrameNotification
        // 键盘显示时发出的通知
        //    UIKeyboardWillShowNotification
        //    UIKeyboardDidShowNotification
        // 键盘隐藏时发出的通知
        //    UIKeyboardWillHideNotification
        //    UIKeyboardDidHideNotification

    在这里我需要实现的效果(如下图)是在在键盘弹出时,使下方的toolbar向上移动到相应位置,因此需要知道键盘的高度和弹出动画的时间,通过广播监听来得到键盘的frame和弹出动画时间:

    NSString *duration = userInfo[UIKeyboardAnimationDurationUserInfoKey];
        CGRect keyboardFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    再通过动画效果,改变tableview和toolbar的frame,使得键盘在弹出时不会被遮挡:

     [UIView animateWithDuration:[duration doubleValue] delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
            _tableview.frame = CGRectMake(0, 64 , SIZE.width, SIZE.height - 64 - keyboardFrame.size.height - 50);
            footView.frame = CGRectMake(0, SIZE.height - keyboardFrame.size.height - 50, SIZE.width, 50);
        } completion:^(BOOL finished) {
            NSIndexPath *path = [NSIndexPath indexPathForRow:_dataArray.count - 1 inSection:0];
            [_tableview scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        }];

    效果图:

  • 相关阅读:
    安装、升级pip,但是python -m pip install --upgrade pip报错
    架构即未来阅读笔记3
    第十二周学习总结
    《大型网站技术架构:核心原理与案分析》阅读笔记02
    2021寒假(12)
    2021寒假(10)
    Spark简介
    《大型网站技术架构:核心原理与案分析》阅读笔记01
    2021寒假(9)
    2021寒假(8)
  • 原文地址:https://www.cnblogs.com/moxuexiaotong/p/4928944.html
Copyright © 2011-2022 走看看