使用键盘显示动画视图(按钮)
Animate a view (button) with the keyboard showing
我的主视图底部有一个按钮,我想在它显示时用键盘将其设置为动画。
我的按钮是这样设置的,在我的情节提要中有一个约束:
我已经在代码中链接了约束:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *myConstraint;
添加了键盘显示时的通知:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
这里是 keyboardWillShow
实现:
- (void)keyboardWillShow: (NSNotification *)notification {
NSDictionary *dictionary = notification.userInfo;
CGRect keyboardFrame = [dictionary[UIKeyboardFrameEndUserInfoKey] CGRectValue];
[UIView animateWithDuration:1.0f animations:^{
self.myConstraint.constant = keyboardFrame.size.height + 18;
}];
}
虽然可以正常工作,但该按钮不会设置动画,而是会立即设置在其在视图中的新位置。我尝试添加一个完成块以查看会发生什么,实际上立即调用完成而不是等待一秒钟......
怎么了?感谢您的帮助。
我想你错过了 layoutIfNeed
的 UIView
你想用新的约束值更新。
你应该试试看。
尝试将动画延迟减少到 0.3。键盘的速度动画比你的按钮的速度动画更大。
您还需要 setNeedsLayout 到您的按钮。
需要动画的是约束变化触发的布局....
self.myConstraint.constant = keyboardFrame.size.height + 18;
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:1.0f animations:^{
[self.view layoutIfNeeded];
}];
如果有人在 Swift 5.3 中寻找相同的结果:
@IBOutlet weak var loginButtonConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
// MARK: - Functions
@objc func keyboardWillShow(notification: NSNotification) {
guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
self.loginButtonConstraint.constant = keyboardSize.height + 20
self.view.setNeedsUpdateConstraints()
UIView.animate(withDuration: 0.2,
animations: {
self.view.layoutIfNeeded()
})
}
@objc func keyboardWillHide(notification: NSNotification) {
self.loginButtonConstraint.constant = 20
self.view.setNeedsUpdateConstraints()
UIView.animate(withDuration: 0.2,
animations: {
self.view.layoutIfNeeded()
})
}
我的主视图底部有一个按钮,我想在它显示时用键盘将其设置为动画。
我的按钮是这样设置的,在我的情节提要中有一个约束:
我已经在代码中链接了约束:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *myConstraint;
添加了键盘显示时的通知:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
这里是 keyboardWillShow
实现:
- (void)keyboardWillShow: (NSNotification *)notification {
NSDictionary *dictionary = notification.userInfo;
CGRect keyboardFrame = [dictionary[UIKeyboardFrameEndUserInfoKey] CGRectValue];
[UIView animateWithDuration:1.0f animations:^{
self.myConstraint.constant = keyboardFrame.size.height + 18;
}];
}
虽然可以正常工作,但该按钮不会设置动画,而是会立即设置在其在视图中的新位置。我尝试添加一个完成块以查看会发生什么,实际上立即调用完成而不是等待一秒钟......
怎么了?感谢您的帮助。
我想你错过了 layoutIfNeed
的 UIView
你想用新的约束值更新。
你应该试试看。
尝试将动画延迟减少到 0.3。键盘的速度动画比你的按钮的速度动画更大。
您还需要 setNeedsLayout 到您的按钮。
需要动画的是约束变化触发的布局....
self.myConstraint.constant = keyboardFrame.size.height + 18;
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:1.0f animations:^{
[self.view layoutIfNeeded];
}];
如果有人在 Swift 5.3 中寻找相同的结果:
@IBOutlet weak var loginButtonConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
// MARK: - Functions
@objc func keyboardWillShow(notification: NSNotification) {
guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
self.loginButtonConstraint.constant = keyboardSize.height + 20
self.view.setNeedsUpdateConstraints()
UIView.animate(withDuration: 0.2,
animations: {
self.view.layoutIfNeeded()
})
}
@objc func keyboardWillHide(notification: NSNotification) {
self.loginButtonConstraint.constant = 20
self.view.setNeedsUpdateConstraints()
UIView.animate(withDuration: 0.2,
animations: {
self.view.layoutIfNeeded()
})
}