UIView 动画只工作一次而不是不工作

UIView Animation just work one time than it doesn't work

我尝试为我的视图制作动画,但如果只是在第一次工作时 isOpen = true 它可以工作但是当我再次调用我的函数时 isOpen = false 没有任何变化吗?

当前视图为自身 (UIView)。 Child 是标签(UILabel)。

private func expansionView(isOpen: Bool) {
        if isOpen {
            label.backgroundColor = .white
            NSLayoutConstraint.activate([
                 label.centerYAnchor.constraint(equalTo: self.topAnchor),
                 label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 15),
             ])
            
            UIView.animate(withDuration: 1, animations: {
                self.layoutIfNeeded()
            }) { (_) in
                
            }
        } else {
            label.backgroundColor = .clear
            NSLayoutConstraint.activate([
                 label.centerYAnchor.constraint(equalTo: self.centerYAnchor),
                 label.leadingAnchor.constraint(equalTo: self.leadingAnchor , constant: 15),
             ])

            UIView.animate(withDuration: 1, animations: {
                self.layoutIfNeeded()
            }) { (_) in
                print("Animation Completed!!!")
            }
        }
        
    }

您需要有两个约束来激活和停用...

lazy var centerXConstraint = label.centerYAnchor.constraint(equalTo: self.centerYAnchor)
lazy var topConstraint = label.leadingAnchor.constraint(equalTo: self.leadingAnchor , constant: 15)
    
    
    init() {
        label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 15).isActive = true
    }
    private func expansionView(isOpen: Bool) {
        if isOpen {
            label.backgroundColor = .white
            
            centerXConstraint.isActive = false
            topConstraint.isActive = true
            
            UIView.animate(withDuration: 1, animations: {
                self.layoutIfNeeded()
            }) { (_) in
                
            }
        } else {
            label.backgroundColor = .clear
            
              centerXConstraint.isActive = true
              topConstraint.isActive = false
            UIView.animate(withDuration: 1, animations: {
                self.layoutIfNeeded()
            }) { (_) in
                print("Animation Completed!!!")
            }
        }
        
    }

您需要有两个约束才能激活其中一个。 您还可以动画更改标签的背景颜色以清除。您可以像下面这样简化您的功能。

// define both vertical constraints
var constraintToCenterYAnchor: NSLayoutConstraint!
var constraintToTopAnchor: NSLayoutConstraint!

// where you init your view..
init() {
    // init your constraints
    constraintToTopAnchor = label.centerYAnchor.constraint(equalTo: topAnchor)
    constraintToCenterYAnchor = label.centerYAnchor.constraint(equalTo: centerYAnchor)

    // set and activate other constraints once.
    label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 15).isActive = true

    // update background color of the label
    label.backgroundColor = .clear
    label.layer.backgroundColor = UIColor.white.cgColor
}

// simplify your function
private func expansionView(_ isOpen: Bool) {
    constraintToTopAnchor.isActive = isOpen
    constraintToCenterYAnchor.isActive = !isOpen
    
    UIView.animate(withDuration: 1) {
        self.label.layer.opacity = isOpen ? 1.0 : 0.0
        self.layoutIfNeeded()
    }
}