The animation that moves the View to match the keyboard is not working.
After clicking textField as shown in images 1 to 3, the Keyboard is displayed, and the View is moved on top of it, but after the Keyboard is displayed, the View is moved late.
Also, if you enter the characters on the keyboard, the predictive transformation hides the View.
If you know the cause, I would appreciate it if you could give me advice.
①
②
③
The source is as follows:
override func viewWillAppear(_animated:Bool){
super.viewWillAppear(true)
NotificationCenter.default.addObserver(self,selector:#selector(handleKeyboardWillShowNotification), name:Notification.Name.UIKeyboardDidShow, object:nil)
NotificationCenter.default.addObserver(self,selector:#selector(handleKeyboardWillHideNotification), name:Notification.Name.UIKeyboardDidHide, object:nil)
}
funchandleKeyboardWillShowNotification(_notification:Notification){
let keyboardFrame=(notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue) ?.cgRectValue
let duration=notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey]
UIView.animate(withDuration: duration as!TimeInterval, animations: {
//self.view.frame.origin.y=-(keyboardFrame?.height)!
let transform = CGAffineTransform (translationX:0, y:-(keyboardFrame?.size.height)!)
self.view.transform=transform
//self.view.layoutIfNeed()
}, completion:nil)
}
functionalKeyboardWillHideNotification(_notification:Notification){
let duration=notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as!Double
UIView.animate(withDuration:duration, animations:{
//self.view.frame.origin.y = 0
let transform = CGAffineTransform (translationX:0, y:0)
self.view.transform=transform
//self.view.layoutIfNeed()
}, completion:nil)
}
View moves late after Keyboard is displayed.
It seems that changing the Notification you receive will solve the problem as follows:
override func viewWillAppear(_animated:Bool){
super.viewWillAppear(true)
// Notification.Name.UIkeyboardDidShow->.UIkeyboardWillShow
NotificationCenter.default.addObserver(self,selector:#selector(handleKeyboardWillShowNotification), name:Notification.Name.UIKeyboardWillShow, object:nil)
// Notification.Name.UIkeyboardDidHide ->UIkeyboardWillHide
NotificationCenter.default.addObserver(self,selector:#selector(handleKeyboardWillHideNotification), name:Notification.Name.UIKeyboardWillHide, object:nil)
}
If you enter the characters on the keyboard, the predictive transformation hides the View.
You can also change the information you want to retrieve from the Notification you received.
funchandleKeyboardWillShowNotification(_notification:Notification){
// UIKeyboardFrameBeginUserInfoKey->UIKeyboardFrameEndUserInfoKey
let keyboardFrame=(notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue) ?.cgRectValue
let duration=notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey]
UIView.animate(withDuration: duration as!TimeInterval, animations: {
let transform = CGAffineTransform (translationX:0, y:-(keyboardFrame?.size.height)!)
self.view.transform=transform
}, completion:nil)
}
The behavior of Notification and Notification Info before and after the event changes depending on how we adopt them.
Below is a summary of the changes.
override func viewWillAppear(_animated:Bool){
super.viewWillAppear(true)
NotificationCenter.default.addObserver(self,selector:#selector(ViewController.handleKeyboardWillShowNotification(_:))),name:.UIKeyboardWillShow,object:nil)
NotificationCenter.default.addObserver(self,selector:#selector(ViewController.handleKeyboardWillHideNotification(_:))), name: .UIKeyboardWillHide, object:nil)
}
override func viewDidDisappear(_animated:Bool){
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self,name:.UIkeyboardWillShow,object:nil)
NotificationCenter.default.removeObserver(self,name:.UIkeyboardWillHide,object:nil)
}
funchandleKeyboardWillShowNotification(_notification:Notification){
guardlet keyboardFrame=(notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue) ?.cgRectValue,
let duration=notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval else {
return
}
UIView.animate(withDuration:duration, animations:{
let transform = CGAffineTransform (translationX:0, y:-(keyboardFrame.size.height))
self.view.transform=transform
}, completion : nil )
}
functionalKeyboardWillHideNotification(_notification:Notification){
guardlet duration=notification.userInfo?[UIKeyboardAnimationUserInfoKey] as? Double else {
return
}
UIView.animate(withDuration:duration, animations:{
let transform = CGAffineTransform (translationX:0, y:0)
self.view.transform=transform
}, completion : nil )
}
© 2024 OneMinuteCode. All rights reserved.