iPad键盘适配

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 {
    /// 隐藏
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值