簡體   English   中英

UIButton的背面動畫不起作用

[英]Back animation of UIButton doesn't work

我有一個文本視圖(textView)和一個按鈕(sendButton)。 按鈕的底部約束是對視圖底部的約束。 textView成為viewDidAppear中的第一個響應者。 因此,當我提出控制器時,鍵盤會上升,並且按鈕也會隨之動畫。

這是代碼:

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

至此,一切正常。
我的問題是,當我關閉鍵盤並結束編輯時,我想重新設置動畫,以便按鈕的底部約束再次成為視圖的底部約束。
但這是行不通的。

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

動畫按鈕功能: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)
}

您必須先刪除之前的底部約束,然后再添加其他約束

或改變

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM