(Swift)键盘唤起时,UITextField视图上移

1)使用方法

// MARK: 键盘弹出时,上移动画效果
func textFieldDidBeginEditing(_ textField: UITextField) {
    self.view.keyBoardMovementAnimation(up: true, offset: 100)
}

// MARK: 键盘收起时,下移恢复至原位置
func textFieldDidEndEditing(_ textField: UITextField) {
    self.view.keyBoardMovementAnimation(up: false, offset: 100)
}

2)源码解析

// 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()
}

你可能感兴趣的:((Swift)键盘唤起时,UITextField视图上移)