gpt4 book ai didi

ios - 将自动布局约束常量 X 动画化到父 View 的中心

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:44:05 25 4
gpt4 key购买 nike

我会为您简短而详细地说明。到目前为止我取得的成功:

1) 我为所需的约束(中心 X 对齐)创建了一个导出。我不知道这是否重要,但我根据需要更改了乘数 (3:1)

enter image description here

2)动画他的常量

UIView.animateWithDuration(0.5, animations: { () -> Void in

self.btn_option_layout.constant = self.nan_view.center.x //Get center x value
self.nan_view.layoutIfNeeded() // Parent View

})

问题:

对象的来源位置:

enter image description here

而不是像这样将 View 动画化到中心 X:

enter image description here

它出于某种原因使他向左移动,这是结果:

enter image description here

我希望我说得足够清楚了,伙计们。感谢您的宝贵时间。

计算约束常数以使其在 X 上居中的正确方法是什么?

最佳答案

您正在尝试做的是完全正确的。您有一个约束将一个 View 的中心固定到另一个 View 的中心。如果该约束的乘数为 1,则该 View 将位于另一个 View 的中心。

但是您的乘数不是 1。而且,正如您正确观察到的那样,您不能更改现有约束的乘数 — 它是只读的。

但是,没问题。简单地用不同的约束替换整个约束,与第一个约束相同,只是它的乘数 1。现在动 Canvas 局!一个鲜为人知的事实是,您可以为这种约束变化设置动画。

这是实际代码:

let con2 = NSLayoutConstraint(item: self.con.firstItem, attribute: self.con.firstAttribute, relatedBy: self.con.relation, toItem: self.con.secondItem, attribute: self.con.secondAttribute, multiplier: 1, constant: 0)
NSLayoutConstraint.deactivateConstraints([self.con])
NSLayoutConstraint.activateConstraints([con2])
UIView.animateWithDuration(1, animations: {self.view.layoutIfNeeded()})

这里是一个循环的 gif,显示了生成的动画到中心:

enter image description here

关于ios - 将自动布局约束常量 X 动画化到父 View 的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30333538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com