首页 > 解决方案 > 如何缓解 iOS 图表中的高亮动画

问题描述

当突出显示 PieChartView 的一部分(来自iOS Charts)时,突出显示的选择默认情况下会在没有动画的情况下跳出。有没有办法缓解标准和突出效果之间的动画?

标准外观:

标准饼图

突出的外观

突出显示

标签: iosswiftanimationios-charts

解决方案


所选段的大小由数据集的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)
}

推荐阅读