繁体   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