From 46b506c60a9102702bd0252e0b2db3492888c349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=B2=94=EC=88=98?= Date: Thu, 16 Aug 2018 23:48:43 +0900 Subject: [PATCH] bind to frameVarible if and only if app is active MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 김범수 --- Sources/RxKeyboard/RxKeyboard.swift | 61 ++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/Sources/RxKeyboard/RxKeyboard.swift b/Sources/RxKeyboard/RxKeyboard.swift index 951ead6..f41c9d0 100644 --- a/Sources/RxKeyboard/RxKeyboard.swift +++ b/Sources/RxKeyboard/RxKeyboard.swift @@ -44,20 +44,20 @@ public class RxKeyboard: NSObject, RxKeyboardType { // MARK: Private + private let frameVariable = BehaviorRelay(value: CGRect( + x: 0, + y: UIScreen.main.bounds.height, + width: UIScreen.main.bounds.width, + height: 0 + )) private let disposeBag = DisposeBag() + private var keyboardChangeDisposeBag = DisposeBag() private let panRecognizer = UIPanGestureRecognizer() // MARK: Initializing override init() { - let defaultFrame = CGRect( - x: 0, - y: UIScreen.main.bounds.height, - width: UIScreen.main.bounds.width, - height: 0 - ) - let frameVariable = BehaviorRelay(value: defaultFrame) self.frame = frameVariable.asDriver().distinctUntilChanged() self.visibleHeight = self.frame.map { UIScreen.main.bounds.height - $0.origin.y } self.willShowVisibleHeight = self.visibleHeight @@ -69,6 +69,41 @@ public class RxKeyboard: NSObject, RxKeyboardType { self.isHidden = self.visibleHeight.map({ $0 == 0.0 }).distinctUntilChanged() super.init() + bindChanges() + + // gesture recognizer + self.panRecognizer.delegate = self + NotificationCenter.default.rx.notification(.UIApplicationDidFinishLaunching) + .map { _ in Void() } + .startWith(Void()) // when RxKeyboard is initialized before UIApplication.window is created + .subscribe(onNext: { _ in + UIApplication.shared.windows.first?.addGestureRecognizer(self.panRecognizer) + }) + .disposed(by: self.disposeBag) + + NotificationCenter.default.rx.notification(.UIApplicationWillResignActive) + .map { _ in Void() } + .subscribe(onNext: { [weak self] _ in + self?.keyboardChangeDisposeBag = DisposeBag() + }) + .disposed(by: self.disposeBag) + + NotificationCenter.default.rx.notification(.UIApplicationDidBecomeActive) + .map { _ in Void() } + .subscribe(onNext: { [weak self] _ in + self?.bindChanges() + }) + .disposed(by: self.disposeBag) + } + + private func bindChanges() { + let defaultFrame = CGRect( + x: 0, + y: UIScreen.main.bounds.height, + width: UIScreen.main.bounds.width, + height: 0 + ) + // keyboard will change frame let willChangeFrame = NotificationCenter.default.rx.notification(.UIKeyboardWillChangeFrame) .map { notification -> CGRect in @@ -116,17 +151,7 @@ public class RxKeyboard: NSObject, RxKeyboardType { // merge into single sequence Observable.of(didPan, willChangeFrame, willHide).merge() .bind(to: frameVariable) - .disposed(by: self.disposeBag) - - // gesture recognizer - self.panRecognizer.delegate = self - NotificationCenter.default.rx.notification(.UIApplicationDidFinishLaunching) - .map { _ in Void() } - .startWith(Void()) // when RxKeyboard is initialized before UIApplication.window is created - .subscribe(onNext: { _ in - UIApplication.shared.windows.first?.addGestureRecognizer(self.panRecognizer) - }) - .disposed(by: self.disposeBag) + .disposed(by: keyboardChangeDisposeBag) } }