4

I am trying to call a function continually over a specified period of time by passing through the progress indicator (Double) should change from 0 to 1 over that period of time. Is there a way to "animate" such change from 0 to 1 in Swift?

UPDATE:

Here is my code for updating the data for a pie chart based on https://github.com/danielgindi/ios-charts

internal func animateDataChange(fromDataSet: PieChartDataSet, toDataSet: PieChartDataSet, progress: Double) -> PieChartDataSet {
    var dataEntries: [ChartDataEntry] = []
    for i in 0..<fromDataSet.yVals.count {
        let yValue = (toDataSet.yVals[i].value - fromDataSet.yVals[i].value) * progress + fromDataSet.yVals[i].value
        let dataEntry = ChartDataEntry(value: yValue, xIndex: i)
        dataEntries.append(dataEntry)
    }
    let animatedDataSet = PieChartDataSet(yVals: dataEntries, label: "")
    return animatedDataSet
}

What I'm trying to do is to animate the change between the two data sets and the progress of the change is reflected in the "progress" parameter. I need to keep calling the function until "progress" = 1.

luk2302
  • 55,258
  • 23
  • 97
  • 137
Grzegorz Aperliński
  • 848
  • 1
  • 10
  • 23
  • not really, you would need to create a timer that repeatedly calls a method (say every 0.1 second), in that method count how often it got called (1, 2, 3... times). Then multiply the `period` and the `times`. For more help, please post what you have tried so far! – luk2302 Jan 11 '16 at 21:01
  • What I'm trying to do is to animate a change in value distribution in a pie chart based on the ios-charts library. I've added a snippet of the function in question. – Grzegorz Aperliński Jan 11 '16 at 21:07
  • please post the code snippet in your question by editing it in - not in the comment. – luk2302 Jan 11 '16 at 21:08
  • Sorry, I've added my code now. – Grzegorz Aperliński Jan 11 '16 at 21:11
  • Okay, I revoke my previous comment. You should look into how you can *animate* iOS charts in the desired way, do not try to code your own animation but let the framework do the work for you. Unfortunately I have no experience with that framework - therefore cannot help you. – luk2302 Jan 11 '16 at 21:17
  • Thanks for you input. Unfortunately, it seem I can only animate a chart so it gets populated with data, but there doesn't seem to be a built-in way to animate data change from a given data set to another. Unless I'm missing something... – Grzegorz Aperliński Jan 11 '16 at 21:21

2 Answers2

2

I've decided to use NSTimer instead to increment the counter from 0 to 1 every 0.01 sec. I know this might not be the most efficient way, but it works:

var progress: Double = 0
var timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target:self, selector: Selector("updateProgress"), userInfo: nil, repeats: true)

func updateProgress() {
    guard progress <= 1 else {
        timer.invalidate()
        return
    }
    progress += 0.01
}
Community
  • 1
  • 1
Grzegorz Aperliński
  • 848
  • 1
  • 10
  • 23
  • can you please update the whole lines for particular animation of data set that how are you using this 'progress' value? – Arun Kumar Feb 14 '18 at 05:13
  • I don't have access to the project anymore but from what I remember I basically created a new PieChartData instance for every progress step and assigned it to the data property of the PieChartView. The time interval between updates created an illusion of animation. Awfully hacky, but it worked. – Grzegorz Aperliński Feb 20 '18 at 15:49
0

I would look at CAKeyFrameAnimation, and store the text of the Double value that you want to animate in a CATextLayer.

You can animate the string value of a CATextLayer, so you can animate from one string value to another. (I'm assuming that the Double that you want to animate is being presented somewhere as a String.) You can repeat animations, and set intermediate values at specified times.

Aaron Rasmussen
  • 13,082
  • 3
  • 42
  • 43