Created
April 5, 2016 03:19
-
-
Save muukii/a914b5bc2175f389a4348316fdf8acc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// KeyboardObserver.swift | |
// Product | |
// | |
// Created by muukii on 3/17/16. | |
// Copyright © 2016 eure. All rights reserved. | |
// | |
import Foundation | |
import RxSwift | |
import RxCocoa | |
public final class KeyboardObserver { | |
public struct KeyboardInfo { | |
public let frameBegin: CGRect | |
public let frameEnd: CGRect | |
init(notification: NSNotification) { | |
let frameEnd = notification.userInfo?[UIKeyboardFrameEndUserInfoKey]?.CGRectValue | |
let frameBegin = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue | |
self.frameBegin = frameBegin! | |
self.frameEnd = frameEnd! | |
} | |
} | |
public let willChangeFrame = PublishSubject<KeyboardInfo>() | |
public let didChangeFrame = PublishSubject<KeyboardInfo>() | |
public let willShow = PublishSubject<KeyboardInfo>() | |
public let didShow = PublishSubject<KeyboardInfo>() | |
public let willHide = PublishSubject<KeyboardInfo>() | |
public let didHide = PublishSubject<KeyboardInfo>() | |
public init() { | |
NSNotificationCenter.defaultCenter() | |
.rx_notification(UIKeyboardWillChangeFrameNotification) | |
.map { KeyboardInfo(notification: $0) } | |
.bindTo(self.willChangeFrame) | |
.addDisposableTo(self.disposeBag) | |
NSNotificationCenter.defaultCenter() | |
.rx_notification(UIKeyboardDidChangeFrameNotification) | |
.map { KeyboardInfo(notification: $0) } | |
.bindTo(self.didChangeFrame) | |
.addDisposableTo(self.disposeBag) | |
NSNotificationCenter.defaultCenter() | |
.rx_notification(UIKeyboardWillShowNotification) | |
.map { KeyboardInfo(notification: $0) } | |
.bindTo(self.willShow) | |
.addDisposableTo(self.disposeBag) | |
NSNotificationCenter.defaultCenter() | |
.rx_notification(UIKeyboardDidShowNotification) | |
.map { KeyboardInfo(notification: $0) } | |
.bindTo(self.didShow) | |
.addDisposableTo(self.disposeBag) | |
NSNotificationCenter.defaultCenter() | |
.rx_notification(UIKeyboardWillHideNotification) | |
.map { KeyboardInfo(notification: $0) } | |
.bindTo(self.willHide) | |
.addDisposableTo(self.disposeBag) | |
NSNotificationCenter.defaultCenter() | |
.rx_notification(UIKeyboardDidHideNotification) | |
.map { KeyboardInfo(notification: $0) } | |
.bindTo(self.didHide) | |
.addDisposableTo(self.disposeBag) | |
} | |
private let disposeBag = DisposeBag() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In case anyone needs a Swift 5 version: