菜鸟教程小白 发表于 2022-12-11 16:59:57

ios - 圆形 UIView 缩放动画有时会回到正方形


                                            <p><p>我使用自动布局在屏幕中央放置了一个 UIView。这个 UIView 是一个正方形,大小为屏幕宽度的 15%。然后在我的 Controller 上添加cornerRadius:</p>

<pre><code>override func viewDidLayoutSubviews() {
    circle.layer.cornerRadius = circle.frame.size.width / 2.0
}
</code></pre>

<p>然后,当用户单击按钮时,圆形 View 会随着第一个动画缩小。</p>

<pre><code>UIView.animateWithDuration(0.4, delay: 0.1, options: .CurveEaseIn, animations: { () -&gt; Void in
    self.circle.alpha = 0.0
    self.circle.transform = CGAffineTransformMakeScale(0.01, 0.01)
}) { (finished) -&gt; Void in
    scaleUp()
}

private func scaleUp() {
    UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseInOut, animations: { () -&gt; Void in
      self.circle.alpha = 1.0
      self.circle.transform = CGAffineTransformIdentity
    }) { (finished) -&gt; Void in

    }
}
</code></pre>

<p>有时缩小动画无法正常工作。在开始之前,cornerRadius 被删除,我的 UIView 变成了一个正方形。但有时我的动画效果很好,圆圈缩小动画也可以。</p>

<p>此外,放大动画似乎一直运行良好。</p>

<p>我不明白为什么缩小动画一直不起作用。</p>

<p>有什么想法吗?</p>

<p>谢谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在启用 <em>Auto Layout</em> 的情况下进行动画制作时,通常最好在开始动画之前在 View 上调用 <code>layoutIfNeeded</code>,然后在循环中再次调用。</p>

<pre><code>override func viewDidLayoutSubviews() {
    circle.layer.cornerRadius = circle.frame.size.width / 2.0
}

@IBAction func scaleDownUp(sender: AnyObject) {
    self.circle.layoutIfNeeded()
    UIView.animateWithDuration(0.4, delay: 0.1, options: .CurveEaseIn, animations: { () -&gt; Void in
      self.circle.alpha = 0.0
      self.circle.transform = CGAffineTransformMakeScale(0.01, 0.01)
      self.circle.layoutIfNeeded()
    }) { (finished) -&gt; Void in
      self.scaleUp()
    }
}

private func scaleUp() {
    self.circle.layoutIfNeeded()
    UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseInOut, animations: {
      self.circle.alpha = 1.0
      self.circle.transform = CGAffineTransformIdentity
      self.circle.layoutIfNeeded()
    }) { (finished) -&gt; Void in

    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 圆形 UIView 缩放动画有时会回到正方形,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38419133/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38419133/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 圆形 UIView 缩放动画有时会回到正方形