gpt4 book ai didi

ios - ReactiveCocoa : Handling Multiple Unbalanced Keyboard Notifications

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:42:01 27 4
gpt4 key购买 nike

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

最佳答案

我想我已经为您找到了解决方案。可能不是最简单的,但它会起作用,我觉得这是解决问题的“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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com