import UIKit

extension UIViewController {
    func animateKeyboardWillChangeFrame(n: NSNotification?, animations: (keyboardFrameEnd: CGRect) -> Void) {
        if let info = n?.userInfo as? Dictionary<String,AnyObject> {
            let frameBeginValue = info[UIKeyboardFrameBeginUserInfoKey] as NSValue
            let frameEndValue = info[UIKeyboardFrameEndUserInfoKey] as NSValue
            let durationNumber = info[UIKeyboardAnimationDurationUserInfoKey] as NSNumber
            let curveNumber = info[UIKeyboardAnimationCurveUserInfoKey] as NSNumber

            let frameBeginRect = view.convertRect(frameBeginValue.CGRectValue(), fromView: nil)
            let frameEndRect = view.convertRect(frameEndValue.CGRectValue(), fromView: nil)
            let duration = durationNumber.doubleValue as NSTimeInterval
            let options = UIViewAnimationOptions(UInt(curveNumber.integerValue << 16))

            UIView.animateWithDuration(
                duration,
                delay: 0.0,
                options: options,
                animations: { animations(keyboardFrameEnd: frameEndRect) },
                completion: nil)
        }
    }
}

class CustomViewController : UIViewController {
    private var _observers = Array<NSObjectProtocol>()
    
    deinit {
        for o in _observers {
            NSNotificationCenter.defaultCenter().removeObserver(o)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        _observers.append(
            NSNotificationCenter.defaultCenter().addObserverForName(
                UIKeyboardWillChangeFrameNotification,
                object: nil,
                queue: NSOperationQueue.mainQueue(),
                usingBlock: { (notification: NSNotification?) in
                    self._keyboardWillChangeFrame(notification)
            })
        )
    }
    
    func _keyboardWillChangeFrame(n: NSNotification?) {
        animateKeyboardWillChangeFrame(n) { (frameEndRect: CGRect) in
            //move stuff around
        }
    }
}