Swift
func wb_keyboardNotification() {
NotificationCenter.default.rac_addObserver(forName: UIResponder.keyboardDidShowNotification.rawValue, object: nil).deliverOnMainThread().subscribeNext { [weak self] (notification: NSNotification?) in
self?.keyboardWillShow(notification: notification!)
}
NotificationCenter.default.rac_addObserver(forName: UIResponder.keyboardWillChangeFrameNotification.rawValue, object: nil).deliverOnMainThread().subscribeNext { [weak self] (notification: NSNotification?) in
self?.keyboardWillShow(notification: notification!)
}
NotificationCenter.default.rac_addObserver(forName: UIResponder.keyboardWillHideNotification.rawValue, object: nil).deliverOnMainThread().subscribeNext { [weak self] (notification: NSNotification?) in
self?.keyboardWillHide(notification: notification!)
}
}
func wb_keyboardNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object:nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object:nil)
}
@objc func keyboardWillShow(notification: NSNotification) {
UIView.animate(withDuration: wb_duration, animations: {
let keyboardHeight = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
print(keyboardHeight)
}, completion: { (finished: Bool) in
})
}
@objc func keyboardWillHide(notification: NSNotification) {
UIView.animate(withDuration: wb_duration, animations: {
let keyboardHeight = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
print(keyboardHeight)
}, completion: { (finished: Bool) in
})
}
deinit {
NotificationCenter.default.removeObserver(self)
}
Objective-C
- (void)wb_keyboardNotification {
[[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardDidShowNotification object:nil]
deliverOn:[RACScheduler mainThreadScheduler]]
subscribeNext:^(NSNotification *value) {
[UIView animateWithDuration:wb_duration animations:^{
CGFloat height = [value.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
NSLog(@"----------:%f", height);
} completion:^(BOOL finished) {
}];
}];
RACSignal *signal = [RACSignal merge:@[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillChangeFrameNotification object:nil],
[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillHideNotification object:nil],
]];
[[[signal
takeUntil:[self rac_signalForSelector:@selector(viewWillDisappear:)]]
deliverOn:[RACScheduler mainThreadScheduler]]
subscribeNext:^(NSNotification *value) {
[UIView animateWithDuration:wb_duration animations:^{
CGFloat height = [value.name isEqualToString:UIKeyboardWillHideNotification] ? 0 : [value.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
NSLog(@"----------:%f", height);
}];
}];
}
网友评论