swift - 动画时如何以编程方式禁用约束

标签 swift autolayout nslayoutconstraint

我有一个固定宽度固定在左侧的 View 。我希望此 View 通过将其锚定到右侧(从而删除左 anchor )来设置动画(从左向右移动)。我该怎么做呢?

我尝试设置优先级,但我不确定语法。我也尝试禁用约束,但那没有用。

fileprivate func sparkle() {
    let sparkleView = UIView()
    sparkleView.backgroundColor = UIColor.yellow
    sparkleView.alpha = 0.5
    addSubview(sparkleView)
    sparkleView.translatesAutoresizingMaskIntoConstraints = false
    sparkleView.topAnchor.constraint(equalTo: topAnchor).isActive = true
    sparkleView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
    sparkleView.widthAnchor.constraint(equalToConstant: frame.width / 5).isActive = true
    sparkleView.leftAnchor.constraint(equalTo: leftAnchor).priority = UILayoutPriorityDefaultLow // Doesn't work
    sparkleView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
    layoutIfNeeded()
    sparkleView.leftAnchor.constraint(equalTo: leftAnchor).isActive = false // Doesn't work
    sparkleView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
    UIView.animate(withDuration: 2, animations: {
        self.layoutIfNeeded()
    }) { (_) in
        sparkleView.removeFromSuperview()
    }
}

目前,由于我们正在分配左右 anchor ,因此宽度限制被打破。

enter image description here

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000020b8eb0 UIView:0x7fc846d551d0.width == 15.35   (active)>

-- 编辑--

我已经尝试创建一个实例变量来保存约束。

let leftCons = sparkleView.leftAnchor.constraint(equalTo: leftAnchor)
leftCons.isActive = true

然后我尝试修改优先级

leftCons.priority = UILayoutPriorityDefaultLow

然而,这会导致错误

'Mutating a priority from required to not on an installed constraint (or vice-versa) is not supported.  You passed priority 250 and the existing priority was 1000.'

相反,我需要将其设置为非事件状态(根据答案)

leftCons.active = false

-- 如果有兴趣,请更正代码.. --

fileprivate func sparkle() {
    let sparkleView = UIView()
    sparkleView.backgroundColor = UIColor.yellow
    sparkleView.alpha = 0.5
    addSubview(sparkleView)
    sparkleView.translatesAutoresizingMaskIntoConstraints = false
    sparkleView.topAnchor.constraint(equalTo: topAnchor).isActive = true
    sparkleView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
    sparkleView.widthAnchor.constraint(equalToConstant: frame.width / 5).isActive = true
    let leftCons = sparkleView.leftAnchor.constraint(equalTo: leftAnchor)
    leftCons.isActive = true
    layoutIfNeeded()
    leftCons.isActive = false
    sparkleView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
    UIView.animate(withDuration: 0.5, animations: {
        self.layoutIfNeeded()
    }) { (_) in
        sparkleView.removeFromSuperview()
    }
}

最佳答案

你需要创建一个变量

var leftCon:NSLayoutConstraint!

然后改变它,你的问题是每一行都创建一个新的约束

sparkleView.widthAnchor.constraint(equalToConstant: frame.width / 5).isActive = true
sparkleView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
sparkleView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true

note: above 3 constraints create a conflict as the view can't be pinned to left and right at the same time with a width constraint that why you see the conflict break => NSLayoutConstraint:0x6000020b8eb0 UIView:0x7fc846d551d0.width == 15.35 (active)

不考虑主动赋值所以那些

sparkleView.leftAnchor.constraint(equalTo: leftAnchor).priority = UILayoutPriorityDefaultLow // Doesn't work 
sparkleView.leftAnchor.constraint(equalTo: leftAnchor).isActive = false // Doesn't work

正在运行并且不改变创建的约束,所以是

leftCon = sparkleView.leftAnchor.constraint(equalTo: leftAnchor)
leftCon.isActive = true

关于这次崩溃

'Mutating a priority from required to not on an installed constraint (or vice-versa) is not supported

激活前需要设置优先级

leftCon.priority = UILayoutPriorityDefaultLow
leftCon.isActive = true

关于swift - 动画时如何以编程方式禁用约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54259910/

相关文章:

ios - Xcode swift 失败,退出代码为 254

iOS - 在运行时找出 View 的约束

ios - 动画约束变化,位置动画但高度跳跃没有动画

ios - 将 PDF 图像添加到 UIBarItem Swift

由于包含 "incomplete"表情符号的字符串,Swift 4 base64 字符串到数据无法正常工作

ios - 自动布局 Xcode。 iPhone 模拟器与 Xcode 预览不匹配

ios - AutoLayout 约束以适应矩形内的 View ,保留一定的纵横比(以编程方式)

ios - 将 Storyboard中的 View 定位在屏幕尺寸的 1/3 处

ios - UITableViewCell 中 iOS7 上的自动布局约束问题

swift - 如何基于类中的其他属性创建变量属性