//当键盘弹起的时候会向系统发出一个通知, //这个时候需要注册一个监听器响应该通知 NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil) //当键盘收起的时候会向系统发出一个通知, //这个时候需要注册另外一个监听器响应该通知 NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide(_:)), name:UIKeyboardWillHideNotification, object: nil)
var keyBoardNeedLayout: Bool = true
func keyboardWillShow(notification: NSNotification) {
print("show")
if let userInfo = notification.userInfo,
value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue,
duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double,
curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt {
let frame = value.CGRectValue()
let intersection = CGRectIntersection(frame, self.view.frame)
let deltaY = CGRectGetHeight(intersection)
if keyBoardNeedLayout {
UIView.animateWithDuration(duration, delay: 0.0,
options: UIViewAnimationOptions(rawValue: curve),
animations: { _ in
self.view.frame = CGRectMake(0,-deltaY,self.view.bounds.width,self.view.bounds.height)
self.keyBoardNeedLayout = false
self.view.layoutIfNeeded()
}, completion: nil)
}
}
}
func keyboardWillHide(notification: NSNotification) {
print("hide")
if let userInfo = notification.userInfo,
value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue,
duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double,
curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt {
let frame = value.CGRectValue()
let intersection = CGRectIntersection(frame, self.view.frame)
let deltaY = CGRectGetHeight(intersection)
UIView.animateWithDuration(duration, delay: 0.0,
options: UIViewAnimationOptions(rawValue: curve),
animations: { _ in
self.view.frame = CGRectMake(0,deltaY,self.view.bounds.width,self.view.bounds.height)
self.keyBoardNeedLayout = true
self.view.layoutIfNeeded()
}, completion: nil)
}
}
self.view.frame = CGRectMake(0,-deltaY,self.view.bounds.width,self.view.bounds.height)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有