// MARK: 键盘弹出时,上移动画效果
func textFieldDidBeginEditing(_ textField: UITextField) {
self.view.keyBoardMovementAnimation(up: true, offset: 100)
}
// MARK: 键盘收起时,下移恢复至原位置
func textFieldDidEndEditing(_ textField: UITextField) {
self.view.keyBoardMovementAnimation(up: false, offset: 100)
}
// MARK: 键盘弹出时,UITextField上移动画
func keyBoardMovementAnimation(up: Bool, offset: CGFloat) {
let moveTime: TimeInterval = 0.3
let movement: CGFloat = ( up ? -offset : offset)
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(moveTime )
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
UIView.commitAnimations()
}
网友评论