ios - 在 iOS 中逐渐添加矩形动画
<p><p>我在 Xcode 中有一个单 View 应用程序,并在 ViewController.swift 中执行以下操作。我的目标是让屏幕在 5 秒内逐渐填满棕色矩形。我错过了什么还是方法完全错误?</p>
<pre><code>override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let expandAnimation: CABasicAnimation = CABasicAnimation(keyPath: "path")
expandAnimation.fromValue = UIBezierPath(rect: CGRect(x: 0.0, y: 0.0, width: 0.0, height: 0.0))
expandAnimation.toValue = UIBezierPath(rect: CGRect(x: 0.0, y: 0.0, width: self.view.bounds.size.width, height: self.view.bounds.size.height))
expandAnimation.duration = 5.0
expandAnimation.fillMode = kCAFillModeForwards
expandAnimation.isRemovedOnCompletion = false
let rectLayer: CAShapeLayer = CAShapeLayer()
rectLayer.fillColor = UIColor.brown.cgColor
rectLayer.path = UIBezierPath(rect: CGRect(x: 0.0, y: 0.0, width: 0.0, height: 0.0)).cgPath
rectLayer.add(expandAnimation, forKey: nil)
self.view.layer.addSublayer(rectLayer)
}
</code></pre>
<p>我不希望颜色逐渐出现。我想要一种玻璃填充的效果。想象一下这个</p>
<p> <a href="/image/SKx1o.gif" rel="noreferrer noopener nofollow"><img src="/image/SKx1o.gif" alt="Water Filling Fancy"/></a> </p>
<p>除了花哨的波浪效果。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>更好地理解您的问题后,您想模拟水杯的归档(没有波浪的动画),您可以通过以下方式实现:</p>
<pre><code> let myLayer: CALayer = .init()
myLayer.backgroundColor = UIColor.red.cgColor
self.view.layer.addSublayer(myLayer)
func animateFilling(to view:UIView)
{
var mFrame = CGRect(x: 0, y: view.frame.size.height, width: view.frame.size.width, height: 0)
myLayer.frame = mFrame
let fillingAnim = CABasicAnimation(keyPath: "bounds")
fillingAnim.fillMode = kCAFillModeForwards
fillingAnim.fromValue = mFrame
fillingAnim.duration = 2
fillingAnim.isRemovedOnCompletion = false
mFrame.size.height = 1000
fillingAnim.toValue = mFrame
myLayer.add(fillingAnim, forKey: nil)
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在 iOS 中逐渐添加矩形动画,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/47988399/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/47988399/
</a>
</p>
页:
[1]