OStack程序员社区-中国程序员成长平台

标题: ios - ReactiveCocoa : Handling Multiple Unbalanced Keyboard Notifications [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:34
标题: ios - ReactiveCocoa : Handling Multiple Unbalanced Keyboard Notifications

我正在使用 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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4