• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - ReactiveCocoa : Handling Multiple Unbalanced Keyboard Notifications

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:34:46 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用 Reactive Cocoa 构建一个示例身份验证 View Controller 。我知道如何以 react 方式设置和接收来自键盘的通知。但是,我收到不平衡的 UP 和 DOWN 通知。正因为如此,我必须设置一个 BOOL 变量来查看键盘之前是否已经抬起但没有放下。有没有办法被动地做到这一点?完整的项目是here .

- (void)configureKeyboardAnimations {
CGFloat duration = 0.9, damping = 0.8;
@weakify(self);
[[[NSNotificationCenter.defaultCenter rac_addObserverForName:UIKeyboardWillShowNotification
                                                      object:nil]
  takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification *notification) {
    @strongify(self);
    if (!self.tableViewOffset) {
        CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
        [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:damping
              initialSpringVelocity:0 options:UIViewAnimationOptionAllowAnimatedContent
                         animations:^{
                             self.tableView.frame = CGRectOffset(self.tableView.frame, 0,
                                                                 -CGRectGetHeight(keyboardRect));
                         } completion:^(BOOL finished) {}];
        self.tableViewOffset = YES; //I need this set immediately not at completion. 
    }
}];

[[[NSNotificationCenter.defaultCenter rac_addObserverForName:UIKeyboardWillHideNotification
                                                      object:nil]
  takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification *notification) {
    @strongify(self);
    CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:damping
          initialSpringVelocity:0 options:UIViewAnimationOptionAllowAnimatedContent
                     animations:^{
                         self.tableView.frame = CGRectOffset(self.tableView.frame, 0,
                                                             CGRectGetHeight(keyboardRect));
                     } completion:^(BOOL finished) {}];
    self.tableViewOffset = NO; //I need this set immediately not at completion. 
}];

}



Best Answer-推荐答案


我想我已经为你找到了解决方案。可能不是最简单的,但它会起作用,我觉得这是一种解决问题的“ReactiveCocoa”方式。

RACSignal *keyboardShowSignal = [[NSNotificationCenter.defaultCenter rac_addObserverForName:UIKeyboardWillShowNotification object:nil] takeUntil:self.rac_willDeallocSignal];

RACSignal *keyboardHideSignal = [[NSNotificationCenter.defaultCenter rac_addObserverForName:UIKeyboardWillHideNotification object:nil] takeUntil:self.rac_willDeallocSignal];

RACSignal *latestNotification = [RACSignal merge[keyboardShowSignal, keyboardHideSignal]];
[[[latestNotification map:^id(NSNotification *notification) {
    return notification.userInfo[UIKeyboardFrameBeginUserInfoKey];
}] distinctUntilChanged] subscribeNext:^(NSNumber *rectNumber) {
    // Animate things here with [rectNumber CGRectValue]
}];

RAC(self, tableViewOffset) = [[[latestNotification map:^id(NSNotification *notification) {
    return @([notification.name isEqualToString:UIKeyboardWillShowNotification]);
}] distinctUntilChanged] startWith(NO)];

完整的项目是here .

关于ios - ReactiveCocoa : Handling Multiple Unbalanced Keyboard Notifications,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26370360/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap