NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notif:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardwillhidden(noti:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
func keyboardWillChange(notif:NSNotification){
let userInfo:NSDictionary = notif.userInfo! as NSDictionary;
let keyBoardInfo2: AnyObject? = userInfo.object(forKey: UIKeyboardFrameEndUserInfoKey) as AnyObject;
let endY = keyBoardInfo2?.cgRectValue.size.height
let aTime = userInfo.object(forKey: UIKeyboardAnimationDurationUserInfoKey) as! TimeInterval
UIView.animate(withDuration: aTime) { [weak self]() -> Void in
self?.view.transform = CGAffineTransform(translationX: 0, y: -endY! / 3 * 2)
}
}
func keyboardwillhidden(noti:NSNotification){
let userInfo:NSDictionary = noti.userInfo! as NSDictionary;
let aTime = userInfo.object(forKey: UIKeyboardAnimationDurationUserInfoKey) as! TimeInterval
UIView.animate(withDuration: aTime) { [weak self]() -> Void in
self?.view.transform = CGAffineTransform.identity
}
}