iOS 中的键盘动画
Keyboard animation in iOS
在我的登录视图控制器中,我使用 UITextFieldDelegate 和动画在键盘打开时向上查看。
代码:
在 viewWillAppear() 中
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
在ViewController
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true;
}
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
kbHeight = keyboardSize.height - 30
imageLogo.alpha = 0.0
self.animateTextField(true)
}
}
}
func animateTextField(up: Bool) {
var movement = (up ? -kbHeight : kbHeight)
UIView.animateWithDuration(0.3, animations: {
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
})
}
func keyboardWillHide(notification: NSNotification) {
imageLogo.alpha = 1.0
self.animateTextField(false)
}
func DismissKeyboard(){
self.view.endEditing(true)
}
当我在键盘动画中更改语言时再次运行
我该如何修复它???
谢谢...
我在视图控制器上有两个或更多 UITextField
对于每个 UITextField,我添加 myTextField.delegate = self
在这种情况下,如果您为 hide/show 键盘使用动画效果不佳
解决方案:
将 .delegate = self 仅用于第一个文本字段
效果不错
在我的登录视图控制器中,我使用 UITextFieldDelegate 和动画在键盘打开时向上查看。 代码:
在 viewWillAppear() 中
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
在ViewController
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true;
}
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
kbHeight = keyboardSize.height - 30
imageLogo.alpha = 0.0
self.animateTextField(true)
}
}
}
func animateTextField(up: Bool) {
var movement = (up ? -kbHeight : kbHeight)
UIView.animateWithDuration(0.3, animations: {
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
})
}
func keyboardWillHide(notification: NSNotification) {
imageLogo.alpha = 1.0
self.animateTextField(false)
}
func DismissKeyboard(){
self.view.endEditing(true)
}
当我在键盘动画中更改语言时再次运行 我该如何修复它???
谢谢...
我在视图控制器上有两个或更多 UITextField 对于每个 UITextField,我添加 myTextField.delegate = self
在这种情况下,如果您为 hide/show 键盘使用动画效果不佳
解决方案: 将 .delegate = self 仅用于第一个文本字段 效果不错