iOS - Fill Animation of a UIBezierPath from bottom

2019-06-07 15:49发布

问题:

I have a UIBezierPath inside custom UIView, draw(). I want to fill that path, lets say a rectangle from bottom to top. How can I implement this.

From Here I have seen using CAShapeLayer its possible. That is working fine with animation but filling is not happening from bottom to top on the rectangle. Please guide.

回答1:

Is this what you are asking for?

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