iOS键盘监听事件

1.注册键盘通知事件

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    // 键盘将出现事件监听
    [center addObserver:self selector:@selector(handleKeyboardWillShow:)

                   name:UIKeyboardWillShowNotification

                 object:nil];

    // 键盘将隐藏事件监听
    [center addObserver:self selector:@selector(handleKeyboardWillHide:)

                   name:UIKeyboardWillHideNotification

                 object:nil];

 

2.处理动画

// 键盘出现

- (void)handleKeyboardWillShow:(NSNotification *)paramNotification
{
    NSDictionary *userInfo = [paramNotification userInfo];

    NSValue *animationCurveObject = [userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];

    NSValue *animationDurationObject = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSValue *keyboardEndRectObject = [userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];

    NSUInteger animationCurve = 0;

    double animationDuration = 0.0f;

    CGRect keyboardEndRect = CGRectMake(0,0, 0, 0);

    [animationCurveObject getValue:&animationCurve];

    [animationDurationObject getValue:&animationDuration];

    [keyboardEndRectObject getValue:&keyboardEndRect];

    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];

    CGRect intersectionOfKeyboardRectAndWindowRect = CGRectIntersection(window.frame, keyboardEndRect);

    CGFloat bottomInset = intersectionOfKeyboardRectAndWindowRect.size.height;

    [UIView beginAnimations:@"changeViewContentInset" context:NULL];

    [UIView setAnimationDuration:animationDuration];

    [UIView setAnimationCurve:(UIViewAnimationCurve) animationCurve];

    CGRect tempRect = self.contentView.frame;

    tempRect.origin.y = Main_Screen_Height - bottomInset - 260 - 20;

    self.contentView.frame = tempRect;

    [UIView commitAnimations];
}
// 键盘消失

- (void)handleKeyboardWillHide:(NSNotification *)paramNotification
{
    NSDictionary *userInfo = [paramNotification userInfo];

    NSValue *animationCurveObject =[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];

    NSValue *animationDurationObject = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSValue *keyboardEndRectObject =[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];

    NSUInteger animationCurve = 0;

    double animationDuration = 0.0f;

    CGRect keyboardEndRect = CGRectMake(0, 0, 0, 0);

    [animationCurveObject getValue:&animationCurve];

    [animationDurationObject getValue:&animationDuration];

    [keyboardEndRectObject getValue:&keyboardEndRect];

    [UIView beginAnimations:@"changeViewContentInset" context:NULL];

    [UIView setAnimationDuration:animationDuration];

    [UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve];

    self.contentView.center = self.center;

    [UIView commitAnimations];
}

 

 3.当然是别忘了注销通知哦

[[NSNotificationCenter defaultCenter] removeObserver:self];

转载于:https://www.cnblogs.com/Milo-CTO/p/6077632.html

### 实现 iOS 设备上软键盘事件监听 在 Vue 项目中处理 iOS 设备上的软键盘事件,可以利用原生的通知机制 `KeyboardWillShowNotification` 和 `KeyboardWillHideNotification` 来捕获键盘的状态变化并相应调整页面布局。 #### 使用 JavaScript 监听键盘事件 对于 iOS 平台,在应用程序初始化阶段设置对特定通知的监听器: ```javascript mounted() { if (/(iPhone|iPad|iPod)/i.test(navigator.userAgent)) { // 判断是否为iOS设备 window.addEventListener('focusin', this.handleFocusIn); window.addEventListener('focusout', this.handleFocusOut); const keyboardShowListener = () => console.log('keyboard will show'); const keyboardHideListener = () => console.log('keyboard will hide'); // 添加键盘显示/隐藏监听 document.addEventListener('native.keyboardshow', keyboardShowListener, false); document.addEventListener('native.keyboardhide', keyboardHideListener, false); // 移除监听函数防止内存泄漏 this.$once('hook:beforeDestroy', () => { document.removeEventListener('native.keyboardshow', keyboardShowListener, false); document.removeEventListener('native.keyboardhide', keyboardHideListener, false); window.removeEventListener('focusin', this.handleFocusIn); window.removeEventListener('focusout', this.handleFocusOut); }); } }, methods: { handleFocusIn(event) { // 处理聚焦逻辑,比如滚动到输入框可见区域 console.log(`Element ${event.target.tagName} got focus`); }, handleFocusOut(event) { // 输入框失去焦点后的操作 console.log(`Element ${event.target.tagName} lost focus`); } } ``` 需要注意的是上述代码中的 `'native.keyboardshow'` 和 `'native.keyboardhide'` 是假设性的自定义事件名称;实际开发环境中可能需要依赖第三方库或插件来适配这些行为[^1]。 另外一种方法是在不借助额外插件的情况下,基于输入域获得和失去焦点来进行简单判断。这种方法适用于只需要知道何时打开关闭而不需要精确高度的情形下[^2]。 为了更精准地控制视图随键盘弹出自动调整位置的行为,还可以考虑引入专门针对移动端优化过的 UI 组件框架或者工具包,它们通常内置了更好的支持与兼容性处理方案[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值