Skip to content

Instantly share code, notes, and snippets.

@jstn
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstn/cbba12d793d4e1fc5dde to your computer and use it in GitHub Desktop.
Save jstn/cbba12d793d4e1fc5dde to your computer and use it in GitHub Desktop.
keyboardWillChangeFrame template
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
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment