ios - iOS中逐步添加矩形动画

标签 ios uiviewcontroller swift4 uibezierpath cabasicanimation

我在 Xcode 中有一个单一 View 应用程序,并在 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)
}

我不希望颜色逐渐出现。我想要一种玻璃填充效果。想象一下这个

Water Filling Fancy

除了花哨的波浪效果。

最佳答案

更好地理解你的问题后,你想模拟水杯的填充(没有波浪的动画),你可以通过以下方式实现:

    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)
    }

关于ios - iOS中逐步添加矩形动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47988399/

相关文章:

ios - 无法打开 Promise<[CLPlacemark]>

ios - 使用 CATransition self.dismiss 时隐藏黑色淡入淡出

ios - 我可以强制 iOS Safari 识别手持媒体查询吗?

ios - Firebase 中的观察者未在 swift 应用程序中被调用

objective-c - 可以将现有的 ViewController 与 PerformSegueWithIdentifier 一起使用吗?

ios - 将属性从模态视图 Controller 传递给父级

ios - 在 UIViewController 之间导航时遇到问题

ios - 在 Xcode 8.3 上向 iOS 10 添加角标(Badge)图标

ios - 无法理解这个返回语句

swift - 反射(reflection):在Swift中获取类的静态var?