Skip to content

Instantly share code, notes, and snippets.

@muukii
Created April 5, 2016 03:19
Show Gist options
  • Save muukii/a914b5bc2175f389a4348316fdf8acc9 to your computer and use it in GitHub Desktop.
Save muukii/a914b5bc2175f389a4348316fdf8acc9 to your computer and use it in GitHub Desktop.
//
// 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()
}
@bogdanchinookbook
Copy link

In case anyone needs a Swift 5 version:

import Foundation
import RxSwift
import RxCocoa

struct KeyboardObserver {
    struct KeyboardInfo {
        
        let frameBegin: CGRect
        let frameEnd: CGRect
        
        init(notification: Notification) {
            self.frameBegin = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue ?? CGRect.zero
            self.frameEnd = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue ?? CGRect.zero
        }
    }
    
    private let disposeBag = DisposeBag()
    
    let willChangeFrame = PublishSubject<KeyboardInfo>()
    let didChangeFrame = PublishSubject<KeyboardInfo>()
    
    let willShow = PublishSubject<KeyboardInfo>()
    let didShow = PublishSubject<KeyboardInfo>()
    let willHide = PublishSubject<KeyboardInfo>()
    let didHide = PublishSubject<KeyboardInfo>()
    
    init() {
        
        NotificationCenter.default.rx.notification(UIResponder.keyboardWillChangeFrameNotification)
            .observeOn(MainScheduler.instance)
            .map { KeyboardInfo(notification: $0) }
            .bind(to: self.willChangeFrame)
            .disposed(by: self.disposeBag)
        
        NotificationCenter.default.rx.notification(UIResponder.keyboardDidChangeFrameNotification)
            .observeOn(MainScheduler.instance)
            .map { KeyboardInfo(notification: $0) }
            .bind(to: self.didChangeFrame)
            .disposed(by: self.disposeBag)
        
        NotificationCenter.default.rx.notification(UIResponder.keyboardWillShowNotification)
            .observeOn(MainScheduler.instance)
            .map { KeyboardInfo(notification: $0) }
            .bind(to: self.willShow)
            .disposed(by: self.disposeBag)
        
        NotificationCenter.default.rx.notification(UIResponder.keyboardDidShowNotification)
            .observeOn(MainScheduler.instance)
            .map { KeyboardInfo(notification: $0) }
            .bind(to: self.didShow)
            .disposed(by: self.disposeBag)
        
        NotificationCenter.default.rx.notification(UIResponder.keyboardWillHideNotification)
            .observeOn(MainScheduler.instance)
            .map { KeyboardInfo(notification: $0) }
            .bind(to: self.willHide)
            .disposed(by: self.disposeBag)
        
        NotificationCenter.default.rx.notification(UIResponder.keyboardDidHideNotification)
            .observeOn(MainScheduler.instance)
            .map { KeyboardInfo(notification: $0) }
            .bind(to: self.didHide)
            .disposed(by: self.disposeBag)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment