简体   繁体   中英

Back animation of UIButton doesn't work

I have a text view (textView) and a button (sendButton). The button's bottom constraint is constraint to the view's bottom. The textView becomes the first responder in 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM