iPad键盘适配

版权声明:知识版权是属于全人类的! 欢迎评论与转载!!! https://blog.csdn.net/zhuge1127/article/details/82389612

iPad键盘弹出样式有三种

如何拆分和移动 iPad 上的键盘:
https://support.apple.com/zh-cn/HT207521
在iOS系统中启用和禁用iPad分裂键盘:
https://zh.wikihow.com/%E5%9C%A8iOS%E7%B3%BB%E7%BB%9F%E4%B8%AD%E5%90%AF%E7%94%A8%E5%92%8C%E7%A6%81%E7%94%A8iPad%E5%88%86%E8%A3%82%E9%94%AE%E7%9B%98

  • dock&merge
    这里写图片描述
  • merge&undock
    这里写图片描述
  • split&undock
    这里写图片描述
    键盘弹出时,常常需要解决一些视图跟随的问题。
    但是键盘的通知事件一直比较乱,尤其是在各个键盘样式之间切换的时候。
    在不断的尝试和验证之后,键盘的通知事件应该监听

  • UIKeyboardWillChangeFrameNotification: 仅对弹出键盘进行操作

  • UIKeyboardDidChangeFrameNotification:仅为适配split和undock等样式切换时系统的bug
  • UIKeyboardWillHideNotification: 隐藏键盘事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameDidChange:) name:UIKeyboardDidChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

- (void)keyboardFrameDidChange:(NSNotification *)notification {
    /// 只在undock下的 merge/split之间相互切换时,willChange:方法中没有参数的时候使用
    if (self.canTriggerEnd) {
        [self keyboardWillShow:notification];
    }
}

- (void)keyboardWillShow:(NSNotification *)notification {
    NSDictionary *info = [notification userInfo];
    CGFloat allScreenHeight = [UIScreen mainScreen].bounds.size.height;
    CGRect kbEndFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat beginY = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].origin.y;
    CGFloat endY = kbEndFrame.origin.y;
    CGFloat bottomSpace = 0;
    BOOL isIPad = YES;

    if (isIPad) {
        if ((allScreenHeight == endY) && (beginY < endY)) {
            self.canTriggerEnd = NO;
            return;
        }

        CGFloat currentHeight = endY + kbEndFrame.size.height;
        if (currentHeight == 0) {
            self.canTriggerEnd = YES;
            return;
        } else {
            self.canTriggerEnd = NO;
        }

        BOOL undock = (allScreenHeight != currentHeight);
        if (undock) {
            bottomSpace = allScreenHeight - currentHeight;
        }
    }
    CGFloat keyBoardHeight = MIN(floor(kbEndFrame.size.height), floor(kbEndFrame.size.width)) + bottomSpace;
    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve animationCurve = [info[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];

    UIViewAnimationOptions animationOptions = UIViewAnimationOptionBeginFromCurrentState;
    if (animationCurve == UIViewAnimationCurveEaseIn) {
        animationOptions |= UIViewAnimationOptionCurveEaseIn;
    } else if (animationCurve == UIViewAnimationCurveEaseInOut) {
        animationOptions |= UIViewAnimationOptionCurveEaseInOut;
    } else if (animationCurve == UIViewAnimationCurveEaseOut) {
        animationOptions |= UIViewAnimationOptionCurveEaseOut;
    } else if (animationCurve == UIViewAnimationCurveLinear) {
        animationOptions |= UIViewAnimationOptionCurveLinear;
    }

    [self animationForKeyBoardShowWithHeight:keyBoardHeight timeInterval:animationDuration animationOptions:animationOptions];
}

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

猜你喜欢

转载自blog.csdn.net/zhuge1127/article/details/82389612