OGeek|极客世界-中国程序员成长平台

标题: iOS - 从底部填充 UIBezierPath 的动画 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:43
标题: iOS - 从底部填充 UIBezierPath 的动画

我在自定义 UIView 中有一个 UIBezierPathdraw()。我想填充那条路径,假设是一个从下到上的矩形。我该如何实现。

来自 Here我已经看到使用 CAShapeLayer 是可行的。这对于动画效果很好,但填充不是从矩形的底部到顶部发生的。请指导。



Best Answer-推荐答案


这是你要的吗?

func zoom() {
    let startPath = UIBezierPath(rect: CGRect(x: 0, y: self.frame.size.height-30, width: self.frame.size.width, height: 30))
    let endPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))

    let rectangleLayer = CAShapeLayer()
    rectangleLayer.path = startPath.cgPath
    rectangleLayer.fillColor = UIColor.cyan.cgColor
    self.layer.addSublayer(rectangleLayer)

    let zoomAnimation = CABasicAnimation()
    zoomAnimation.keyPath = "path"
    zoomAnimation.duration = 2.0
    zoomAnimation.toValue = endPath.cgPath
    zoomAnimation.fillMode = kCAFillModeForwards
    zoomAnimation.isRemovedOnCompletion = false
    rectangleLayer.add(zoomAnimation, forKey: "zoom")
}

关于iOS - 从底部填充 UIBezierPath 的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41464101/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4