Swift iOS - 即使调用了 view.layoutIfNeeded(),约束也没有动画

Swift iOS -Constraint isn't animating even though view.layoutIfNeeded() is called

我有一个圆形的 likeButton,位于圆形的 moreButton 后面。 likeButton 固定到 moreButton 的 centerX 和 centerY。当我按下 moreButton 时,我想将 likeButton 设置为高于 moreButton 200 点的动画。

我使用 NSLayoutConstraint 来跟踪 likeButton 的 centerY 并对其进行更改。当我调用 UIView.animate 并在它的闭包中调用 self.view.layoutIfNeeded() 时,视图没有更新。

@objc func moreButtonTapped(){

    likeButtonCenterY?.isActive = false
    likeButton.centerYAnchor.constraint(equalTo: self.moreButton.centerYAnchor, constant: -200)
    likeButtonCenterY?.isActive = true

    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

我哪里错了?

let moreButton: UIButton = {
    let button = UIButton(type: .system)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setImage(UIImage(named: "more"), for: .normal)
    button.tintColor = .red
    button.clipsToBounds = true
    button.addTarget(self, action: #selector(moreButtonTapped), for: .touchUpInside)
    return button
}()

let likeButton: UIButton = {
    let button = UIButton(type: .system)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setImage(UIImage(named: "like"), for: .normal)
    button.tintColor = .blue
    button.clipsToBounds = true
    return button
}()

var likeButtonCenterY: NSLayoutConstraint?

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    setAnchors()
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    moreButton.layer.cornerRadius = moreButton.frame.size.width / 2
    likeButton.layer.cornerRadius = likeButton.frame.size.width / 2
}

@objc func moreButtonTapped(){

    likeButtonCenterY?.isActive = false
    likeButton.centerYAnchor.constraint(equalTo: self.moreButton.centerYAnchor, constant: -200)
    likeButtonCenterY?.isActive = true

    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

func setAnchors(){
    view.addSubview(likeButton)
    view.addSubview(moreButton)

    moreButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    moreButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -200).isActive = true
    moreButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
    moreButton.heightAnchor.constraint(equalToConstant: 100).isActive = true

    likeButtonCenterY = likeButton.centerYAnchor.constraint(equalTo: moreButton.centerYAnchor)
    likeButtonCenterY?.isActive = true

    likeButton.centerXAnchor.constraint(equalTo: moreButton.centerXAnchor).isActive = true
    likeButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
    likeButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
}

如果您同时 activating/deactivating 多个约束(如批处理),您可能需要重写 updateConstraints() 方法。更多信息在这里:https://developer.apple.com/documentation/uikit/uiview/1622512-updateconstraints

基本上,您在 moreButtonTapped() 中的代码将进入 updateConstraints() 方法的重写实现。不要忘记在方法的最后一行调用 super()

那么您的动画代码应该如下所示:

UIView.animate(withDuration: 0.3) {
    self.view.setNeedsUpdateConstraints()
    self.view.layoutIfNeeded()
})

试试这个

@objc func moreButtonTapped(){

likeButtonCenterY?.isActive = false
likeButtonCenterY = likeButton.centerYAnchor.constraint(equalTo: self.moreButton.centerYAnchor, constant: -200)
likeButtonCenterY?.isActive = true

UIView.animate(withDuration: 0.3) {
    self.view.layoutIfNeeded()
}
}

你忘了写likeButtonCenterY =