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

标签 ios swift animation button uibutton

我有一个 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/

相关文章:

ios - 如何在比赛中将数据从 GKPlayer 发送到另一个?

java - 图像动画未显示

xcode - UIButton 如何淡入淡出

ios - HealthKit:是否可以通过查询将两个数量相加?

ios - 推送通知中的应用程序名称错误 | ios SWIFT

iphone - NSManagedObject 发布

ios - UILabel 文字划过角度

swift - 如何在 Firebase 中存储警报文本?

html - 动画转发不适用于 :hover

iOS 8 Swift 应用放大显示