【问题标题】:UIView Animation just work one time than it doesn't workUIView 动画只能工作一次而不是不能工作
【发布时间】:2020-11-12 04:23:10
【问题描述】:

我尝试为我的视图制作动画,但如果只是在第一次工作 isOpen = true 它工作但当我再次调用我的函数时 isOpen = false 什么都没有改变?

Perent 视图是自我的(UIView)。孩子是标签(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!!!")
            }
        }
        
    }

【问题讨论】:

  • 第二次在调试中遇到约束中断?
  • 在我的调试控制台中说可能以下列表中的至少一个约束是您不想要的。试试这个:(1)查看每个约束并尝试找出您不期望的; (2) 找到添加了一个或多个不需要的约束的代码并修复它。将尝试通过打破约束来恢复 <0x60000056c910 uilabel:0x7fc61e605620 authtextfield.authfield:0x7fc61fb04ed0.centery>

标签: swift animation layout constraints


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-10
      • 2017-09-10
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      • 2018-01-28
      相关资源
      最近更新 更多