Swift 3:CAShapeLayer蒙版不为饼图动画制作动画

时间:2017-06-28 01:43:51

标签: ios swift core-graphics core-animation

我有代码可以在给定任意输入数据的情况下成功创建饼图。它会生成一个如下所示的饼图:

Pie chart to animate

我想要的是什么:

我希望创建一个动画,按顺序绘制饼图的那些部分。也就是说,我希望执行饼图的“时钟滑动”动画。屏幕应该从黑色开始,然后逐渐填充以产生上面的饼图图像。

问题:

我正在尝试为每个饼图片创建一个图层蒙版,并为图层蒙版设置动画,以便饼图片填充动画。但是,当我实现图层蒙版时,它没有动画,屏幕仍然是黑色。

代码:

class GraphView: UIView {
    // Array of colors for graph display
    var colors: [CGColor] = [UIColor.orange.cgColor, UIColor.cyan.cgColor, UIColor.green.cgColor, UIColor.blue.cgColor]

    override func draw(_ rect: CGRect) {
        drawPieChart()
    }

    func drawPieChart() {
        // Get data from file, yields array of numbers that sum to 100
        let data: [String] = getData(from: "pie_chart")

        // Create the pie chart
        let pieCenter: CGPoint = CGPoint(x: frame.width / 2, y: frame.height / 2)
        let radius = frame.width / 3
        let rad = 2 * Double.pi
        var lastEnd: Double = 0
        var start: Double = 0

        for i in 0...data.count - 1 {
            let size = Double(data[i])! / 100
            let end: Double = start + (size * rad)

            // Create the main layer
            let path = UIBezierPath()
            path.move(to: pieCenter)
            path.addArc(withCenter: pieCenter, radius: radius, startAngle: CGFloat(start), endAngle: CGFloat(end), clockwise: true)

            let shapeLayer = CAShapeLayer()
            shapeLayer.path = path.cgPath
            shapeLayer.fillColor = colors[i] 

            // Create the mask
            let maskPath = CGMutablePath()
            maskPath.move(to: pieCenter)
            maskPath.addArc(center: pieCenter, radius: radius, startAngle: CGFloat(start), endAngle: CGFloat(end), clockwise: true)

            let shapeLayerMask = CAShapeLayer()
            shapeLayerMask.path = maskPath
            shapeLayerMask.fillColor = colors[i]
            shapeLayerMask.lineWidth = radius
            shapeLayerMask.strokeStart = 0
            shapeLayerMask.strokeEnd = 1

            // Set the mask
            shapeLayer.mask = shapeLayerMask
            shapeLayer.mask!.frame = shapeLayer.bounds

            // Add the masked layer to the main layer
            self.layer.addSublayer(shapeLayer)

            // Animate the mask
            DispatchQueue.main.asyncAfter(deadline: .now() + Double(i)) {
                let animation = CABasicAnimation(keyPath: "strokeEnd")
                animation.fromValue = shapeLayerMask.strokeStart
                animation.toValue = shapeLayerMask.strokeEnd
                animation.duration = 4
                animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
                animation.isRemovedOnCompletion = false
                animation.autoreverses = false

                shapeLayerMask.add(animation, forKey: nil)
            }

            start = end
        }
    }
}

同样,此代码只会产生一个空白屏幕。如果您注释掉与掩码相关的代码并添加shapeLayer作为子视图,您将看到未动画的饼图。

我做错了什么,如何让我的饼图动画?

1 个答案:

答案 0 :(得分:0)

必须先将子图层添加到父图层,然后才能屏蔽它们:

// Code above this line adds the pie chart slices to the parentLayer
for layer in parentLayer.sublayers! {
    // Code in this block creates and animates a mask for each layer
}
// Add the parent layer to the highest parent view/layer

这就是它的全部内容。您必须确保饼图切片和子图层之间存在一对一的对应关系。 这是因为您无法使用图层蒙版的一个实例屏蔽多个视图/图层。执行此操作时,图层蒙版将成为您尝试蒙版的图层Layer Hierarchy的一部分。必须建立掩码和要屏蔽的图层之间的父子关系才能使其生效。