Skip to content

Instantly share code, notes, and snippets.

@mitsuse
Created March 5, 2018 06:27
Show Gist options
  • Save mitsuse/d202820fefe0599c8855db461911e85a to your computer and use it in GitHub Desktop.
Save mitsuse/d202820fefe0599c8855db461911e85a to your computer and use it in GitHub Desktop.
Reactive extension for events emitted from keyboard.
import UIKit
import RxSwift
import RxCocoa
public enum Keyboard: ReactiveCompatible {
public struct Update {
public let size: CGSize
public let duration: Double
}
public struct Transition {
public let last: CGSize
public let next: CGSize
public let duration: Double
}
}
extension Reactive where Base == Keyboard {
public static var willShow: Observable<Keyboard.Update> { return notifications(.UIKeyboardWillShow).map(extract) }
public static var willHide: Observable<Keyboard.Update> { return notifications(.UIKeyboardWillHide).map(extract) }
public static var willChangeFrame: Observable<Keyboard.Update> { return notifications(.UIKeyboardWillChangeFrame).map(extract) }
public static var didShow: Observable<Keyboard.Update> { return notifications(.UIKeyboardDidShow).map(extract) }
public static var didHide: Observable<Keyboard.Update> { return notifications(.UIKeyboardDidHide).map(extract) }
public static var didChangeFrame: Observable<Keyboard.Update> { return notifications(.UIKeyboardDidChangeFrame).map(extract) }
}
extension Reactive where Base == Keyboard {
public static var transitions: Observable<Keyboard.Transition> {
let initial = Keyboard.Update(size: .zero, duration: 0)
return
willChangeFrame
.scan((CGSize.zero, initial)) { state, next in let (_, previous) = state; return (previous.size, next) }
.skip(1)
.map { size, update in Keyboard.Transition(last: size, next: update.size, duration: update.duration) }
}
}
private func notifications(_ name: Notification.Name) -> Observable<Notification> { return NotificationCenter.default.rx.notification(name) }
private func extract(_ notification: Notification) -> Keyboard.Update {
return Keyboard.Update(
size: (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size,
duration: (notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment