[英]Back animation of UIButton doesn't work
I have a text view (textView) and a button (sendButton). 我有一个文本视图(textView)和一个按钮(sendButton)。 The button's bottom constraint is constraint to the view's bottom.
按钮的底部约束是对视图底部的约束。 The textView becomes the first responder in viewDidAppear.
textView成为viewDidAppear中的第一个响应者。 So when i present the Controller, the keyboard goes up and the button animates along with it.
因此,当我提出控制器时,键盘会上升,并且按钮也会随之动画。
Here's the code: 这是代码:
override func viewDidLoad() {
super.viewDidLoad()
setupSendButton()
dismissKeyboard()
}
override func viewDidAppear(_ animated: Bool) {
textView.becomeFirstResponder()
}
func setupSendButton() {
self.view.addSubview(sendButton)
sendButton.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
sendButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
sendButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
sendButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
sendButton.translatesAutoresizingMaskIntoConstraints = false
}
// TextView Delegate Method
func textViewDidBeginEditing(_ textView: UITextView) {
// Animation begins after textView did begin editing
animateSendButton(bottomConstraint: -216)
}
At this point everything works fine. 至此,一切正常。
My problem is that when i dismiss the keyboard and end editing, I want to animate back so that the button's bottom constraint is the view's bottom constraint again. 我的问题是,当我关闭键盘并结束编辑时,我想重新设置动画,以便按钮的底部约束再次成为视图的底部约束。
But that doesn't work. 但这是行不通的。
// TextView Delegate Method
func textViewDidEndEditing(_ textView: UITextView) {
// Animation begins after textView did end editing (it doesn't)
animateSendButton(bottomConstraint: 0)
}
// function to dismiss keyboard and end editing
func dismissKeyboard() {
let touch = UITapGestureRecognizer(target: self, action: #selector(tapGesture))
self.view.addGestureRecognizer(touch)
}
@objc func tapGesture(gesture: UITapGestureRecognizer){
// Ends editing and dismisses keyboard
self.view.endEditing(true)
}
Animate Button Funktion: animateSendButton(bottomConstraint: CGFloat) 动画按钮功能:animateSendButton(底部约束:CGFloat)
func animateSendButton(bottomConstraint: CGFloat) {
sendButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: bottomConstraint).isActive = true
UIView.animate(withDuration: 0.55, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
You must delete previous bottom constraint before of adding other 您必须先删除之前的底部约束,然后再添加其他约束
or change 或改变
var bottomCon:NSLayoutConstraint!
////// //////
func setupSendButton() {
self.view.addSubview(sendButton)
sendButton.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
sendButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
sendButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
self.bottomCon = sendButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
self.bottomCon.active= true
sendButton.translatesAutoresizingMaskIntoConstraints = false
}
func animateSendButton(bottomConstraint: CGFloat) {
self.bottomCon.constant = bottomConstraint
UIView.animate(withDuration: 0.55, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.