UIButton的背景动画不起作用

时间:2018-01-20 13:23:55

标签: ios swift animation button uibutton

我有一个文本视图(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)
}

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

1 个答案:

答案 0 :(得分:0)

在添加其他

之前,您必须删除之前的底部约束

或更改

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)
}
相关问题