如何简化iOS图表中的突出显示动画

时间:2018-08-24 17:50:31

标签: ios swift animation ios-charts

突出显示PieChartView的一部分时(来自iOS Charts),突出显示的选择默认情况下会跳出而不显示动画。有没有一种方法可以减轻标准效果和突出显示效果之间的动画影响?

标准外观:

Standard Pie Chart

突出显示的外观

Highlighted

1 个答案:

答案 0 :(得分:0)

所选段的大小由dataSet的selectionShift属性控制。值0表示选中该元素不会对其进行更改。

可以通过内置的动画器功能控制该值随时间的变化。

要在选择饼图的一部分时触发动画制作器,请实现图表视图委托的chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight)功能。

示例:

override func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {

    let animator = Animator()

    animator.updateBlock = {
        // Usually the phase is a value between 0.0 and 1.0
        // Multiply it so you get the final phaseShift you want
        let phaseShift = 10 * animator.phaseX

        let dataSet = chartView.data?.dataSets.first as? PieChartDataSet
        // Set the selectionShift property to actually change the selection over time
        dataSet?.selectionShift = CGFloat(phaseShift)

        // In order to see the animation, trigger a redraw every time the selectionShift property was changed
        chartView.setNeedsDisplay()
    }

    // Start the animation by triggering the animate function with any timing function you like
    animator.animate(xAxisDuration: 0.3, easingOption: ChartEasingOption.easeInCubic)
}