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.
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
}