gpt4 book ai didi

ios - UIButton 的返回动画不起作用

转载 作者:行者123 更新时间:2023-11-30 11:50:38 25 4
gpt4 key购买 nike

我有一个 TextView (textView)和一个按钮(sendButton)。按钮的底部约束是对 View 底部的约束。textView 成为 viewDidAppear 中的第一响应者。因此,当我展示 Controller 时,键盘会升起,按钮也会随之动画。

代码如下:

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

此时一切正常。
我的问题是,当我关闭键盘并结束编辑时,我想动画回来,以便按钮的底部约束再次成为 View 的底部约束。
但这是行不通的。

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

最佳答案

在添加其他底部约束之前,您必须删除先前的底部约束

或更改

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

关于ios - UIButton 的返回动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48356647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com