Last active
August 29, 2015 14:13
-
-
Save jstn/cbba12d793d4e1fc5dde to your computer and use it in GitHub Desktop.
keyboardWillChangeFrame template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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