iOS - Fill Animation of a UIBezierPath from bottom

2019-06-07 15:16发布

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条回答
老娘就宠你
2楼-- · 2019-06-07 16:12

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")
}
查看更多
登录 后发表回答