菜鸟教程小白 发表于 2022-12-11 20:41:08

ios - 最好的方法是什么,取消约束或更改优先级?


                                            <p><p>我不太了解 AutoLayout。由于我试图用一些 UIElements 做多个动画,我需要停用/激活约束还是应该使用 anchor 的优先级?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以观看<a href="https://developer.apple.com/videos/play/wwdc2015/218/" rel="noreferrer noopener nofollow">WWDC video about Auto Layout tech</a> .自动布局的核心是<a href="https://en.wikipedia.org/wiki/Linear_programming" rel="noreferrer noopener nofollow">Linear Programming</a> .它使用 <a href="http://www.constraintsolving.com/solvers" rel="noreferrer noopener nofollow">constraint solver</a>解决那些线性问题。 </p>

<p>图片来自 <a href="https://en.wikipedia.org/wiki/Linear_programming" rel="noreferrer noopener nofollow">WiKi</a> ,自动布局的那些规则与<code>问题约束</code>相同,并且只有一个全局最优来解决这些约束。如果没有全局最优解来解决这些约束,那么这些规则就有冲突。</p>

<p> <a href="/image/39ths.jpg" rel="noreferrer noopener nofollow"><img src="/image/39ths.jpg" alt="WiKi image"/></a> </p>

<hr/>

<p>从 WWDC 视频中,我学会了定义一组约束来实现动画,方法是停用 A 组,然后激活 B 组。有时我喜欢修改约束的属性来实现动画效果。如果这种方法使我的代码简洁易懂,那么我就采用这种方法。</p>

<h3>属性:isActive</h3>

<p> <a href="https://developer.apple.com/documentation/uikit/nslayoutconstraint/1527000-isactive" rel="noreferrer noopener nofollow">Apple official document</a> </p>

<p>自动布局约束可以从 <code>xib</code> 和 <code>storyboard</code> 绑定(bind)到 <code>@IBOutlet</code>。 <code>NSLayoutConstraint</code> 有一个名为 <a href="https://developer.apple.com/documentation/uikit/nslayoutconstraint/1527000-isactive" rel="noreferrer noopener nofollow">isActive</a> 的属性.您可以定义约束组。但在您激活/停用约束之前,请始终先停用。为了避免约束冲突。我从 WWDC 视频中得到了这个。 </p>

<h3>属性:常数,乘数</h3>

<ul>
<li> <a href="https://developer.apple.com/documentation/uikit/nslayoutconstraint/1526928-constant" rel="noreferrer noopener nofollow">Apple doc: constant</a> </li>
<li> <a href="https://developer.apple.com/documentation/uikit/nslayoutconstraint/1526920-multiplier" rel="noreferrer noopener nofollow">Apple doc: multiplier</a> </li>
</ul>

<p>您可以更改 <a href="https://developer.apple.com/documentation/uikit/nslayoutconstraint/1526920-multiplier" rel="noreferrer noopener nofollow">multiplier</a>和 <a href="https://developer.apple.com/documentation/uikit/nslayoutconstraint/1526928-constant" rel="noreferrer noopener nofollow">constant</a>运行时的约束。更新约束属性后,调用 <a href="https://developer.apple.com/documentation/uikit/uiview/1622450-setneedsupdateconstraints" rel="noreferrer noopener nofollow">setNeedsupdateConstraints</a>和 <a href="https://developer.apple.com/documentation/uikit/uiview/1622595-updateconstraintsifneeded" rel="noreferrer noopener nofollow">updateConstraintsifNeeded</a> <code>UIView</code> 以应用这些更改。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 最好的方法是什么,取消约束或更改优先级?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/52770774/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/52770774/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 最好的方法是什么,取消约束或更改优先级?