UIButton 的后退动画不起作用

Posted

技术标签:

【中文标题】UIButton 的后退动画不起作用【英文标题】:Back animation of UIButton doesn't work 【发布时间】:2018-01-20 13:23:55 【问题描述】:

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

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

或改变

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)

【讨论】:

以上是关于UIButton 的后退动画不起作用的主要内容,如果未能解决你的问题,请参考以下文章

iOS:UIButton 的动画文本位置不起作用

UIButton 子类@IBAction 不起作用

子类化 UIButton 并覆盖触摸事件 - 不起作用

目标 C:后退按钮 segue 不起作用

如果将 UIButton 作为子视图添加到 UIImageView,则单击 UIButton 不起作用

UIButton 在 collectionView 单元格中不起作用