逐渐在iOS中添加矩形动画
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了逐渐在iOS中添加矩形动画相关的知识,希望对你有一定的参考价值。
我在Xcode中有一个视图应用程序,并在ViewController.swift中执行以下操作。我的目标是让屏幕在5秒内逐渐填满棕色矩形。我错过了什么或者方法完全错了吗?
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)
}
我不希望颜色逐渐出现。我想要一种玻璃填充效果。想象一下
除了奇特的波浪效果。
答案
在更好地理解你的问题之后,你想要模拟水玻璃的归档(没有波浪的动画),你可以通过以下方式实现:
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)
}
另一答案
如果你不想要顶级浪潮,我觉得很容易做到这一点
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let vv = UIView(frame: CGRect(x: 0, y: self.view.bounds.size.height, width: self.view.bounds.size.width, height: 0))
vv.backgroundColor = UIColor.brown
self.view.addSubview(vv)
UIView.animate(withDuration: 5.0) {
vv.frame = CGRect(x: 0, y: self.view.bounds.size.height / 3.0, width: self.view.bounds.size.width, height: self.view.bounds.size.height * 2 / 3.0)
}
}
以上是关于逐渐在iOS中添加矩形动画的主要内容,如果未能解决你的问题,请参考以下文章