在iOS中逐渐添加矩形动画
我在Xcode中有一个单视图应用程序,并在ViewController.swift中执行以下操作。我的目标是让屏幕在5秒内逐渐填满一个棕色的矩形。是我遗漏了什么,还是方法完全错了在iOS中逐渐添加矩形动画,ios,uiviewcontroller,swift4,uibezierpath,cabasicanimation,Ios,Uiviewcontroller,Swift4,Uibezierpath,Cabasicanimation,我在Xcode中有一个单视图应用程序,并在ViewController.swift中执行以下操作。我的目标是让屏幕在5秒内逐渐填满一个棕色的矩形。是我遗漏了什么,还是方法完全错了 override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) let expandAnimation: CABasicAnimation = CABasicAnimation(keyPath: "path")
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)
}
我认为如果你不想要top wave,那么这样做很容易
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)
}
}
您想让您的视图逐渐将颜色从一种颜色更改为另一种颜色?从空到完全充满棕色我需要这种波浪效果注水,请指导。如果我的解释不够详细,请原谅。但我不希望颜色逐渐显现。我想要一种与玻璃填充相反的效果。想象一下与之相反的效果,除了奇特的波浪效果。这就是为什么我要使用UIBezierPathThank,感谢您的快速响应!酷,用这个gif更新你的问题,这会让人们更清楚。@AbSin没问题,我已经根据你的新解释编辑了答案。@Shu Khan我认为wave动画比把它放在适当的位置要复杂得多。我将尝试一下,我仍在学习,如果我做不到,我将发布一个单独的问题。谢谢,它也做了这项工作!