Skip to content

Instantly share code, notes, and snippets.

@kirsteins
Last active August 29, 2015 14:17
Show Gist options
  • Save kirsteins/ceb14438357c853cb66f to your computer and use it in GitHub Desktop.
Save kirsteins/ceb14438357c853cb66f to your computer and use it in GitHub Desktop.
KeyboardListener
//
// KeyboardListener.swift
// KeyboardListener
//
// Created by Jānis Kiršteins on 15/03/15.
// Copyright (c) 2015 Jānis Kiršteins. All rights reserved.
//
import UIKit
public struct KeyboardNotificationInfo: Printable {
public let beginFrame: CGRect
public let endFrame: CGRect
public let animationDuration: NSTimeInterval
public let animationCurveRawValue: Int
public var description: String {
return toString([
"beginFrame" : toString(self.beginFrame),
"endFrame" : toString(self.endFrame),
"animationDuration" : self.animationDuration,
"animationCurveRawValue" : self.animationCurveRawValue
])
}
private init(notification: NSNotification) {
let userInfo = notification.userInfo as! [String : AnyObject]
self.animationCurveRawValue = userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber as Int
self.animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber as NSTimeInterval
let beginFrameValue = userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue
self.beginFrame = beginFrameValue.CGRectValue()
let endFrameValue = userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue
self.endFrame = endFrameValue.CGRectValue()
}
}
public class KeyboardListener: NSObject {
public var onWillShow: ((info: KeyboardNotificationInfo) -> ())?
public var onWillHide: ((info: KeyboardNotificationInfo) -> ())?
public var onDidShow: ((info: KeyboardNotificationInfo) -> ())?
public var onDidHide: ((info: KeyboardNotificationInfo) -> ())?
public var onFrameWillChange: ((info: KeyboardNotificationInfo) -> ())?
public var onFrameDidChange: ((info: KeyboardNotificationInfo) -> ())?
public func start() {
let center = NSNotificationCenter.defaultCenter()
center.addObserver(self, selector: "willShow:", name: UIKeyboardWillShowNotification, object: nil)
center.addObserver(self, selector: "willHide:", name: UIKeyboardWillHideNotification, object: nil)
center.addObserver(self, selector: "didShow:", name: UIKeyboardDidShowNotification, object: nil)
center.addObserver(self, selector: "didHide:", name: UIKeyboardDidHideNotification, object: nil)
center.addObserver(self, selector: "frameWillChange:", name: UIKeyboardWillChangeFrameNotification, object: nil)
center.addObserver(self, selector: "frameDidChange:", name: UIKeyboardDidChangeFrameNotification, object: nil)
}
public func stop() {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func willShow(notification: NSNotification) {
self.onWillShow?(info: KeyboardNotificationInfo(notification: notification))
}
func willHide(notification: NSNotification) {
self.onWillHide?(info: KeyboardNotificationInfo(notification: notification))
}
func didShow(notification: NSNotification) {
self.onDidShow?(info: KeyboardNotificationInfo(notification: notification))
}
func didHide(notification: NSNotification) {
self.onDidHide?(info: KeyboardNotificationInfo(notification: notification))
}
func frameWillChange(notification: NSNotification) {
self.onFrameWillChange?(info: KeyboardNotificationInfo(notification: notification))
}
func frameDidChange(notification: NSNotification) {
self.onFrameDidChange?(info: KeyboardNotificationInfo(notification: notification))
}
deinit {
self.stop()
}
}
//
// ViewController.swift
// KeyboardListener
//
// Created by Jānis Kiršteins on 15/03/15.
// Copyright (c) 2015 Jānis Kiršteins. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
let keyboardListener = KeyboardListener()
override func viewDidLoad() {
super.viewDidLoad()
self.keyboardListener.onWillShow = {
println($0)
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.keyboardListener.start()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.keyboardListener.stop()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment