iOS 监听键盘事件
2019-07-25 本文已影响0人
iVikings
Swift
- RAC 监听
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
})
}
- remove observer
deinit {
NotificationCenter.default.removeObserver(self)
}
Objective-C
- RAC 监听
- (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);
}];
}];
}