Advertisement
Revolucent

isFirstResponder observable with RxSwift

Oct 4th, 2018
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.56 KB | None | 0 0
  1. import RxCocoa
  2. import RxSwift
  3. import UIKit
  4.  
  5. extension Reactive where Base: UIView {
  6.     var isFirstResponder: Observable<Bool> {
  7.         return Observable
  8.             .merge(
  9.                 methodInvoked(#selector(UIView.becomeFirstResponder)),
  10.                 methodInvoked(#selector(UIView.resignFirstResponder))
  11.             )
  12.             .map{ [weak view = self.base] _ in
  13.                 view?.isFirstResponder ?? false
  14.             }
  15.             .startWith(base.isFirstResponder)
  16.             .distinctUntilChanged()
  17.             .share(replay: 1)
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement