I use the calcZoom
function to calculate the zoom for linear zooming of a scroll view. But how do I calculate the contentOffset
?
startOffset
is the contentOffset
at startZoom
.
endOffset
is the contentOffset
at endZoom
.
var timerA = Timer()
var currentTime = 0.0
let step = 0.0005
let sDuration = "5.0"
var startZoom = 2.5 //for Example
var endZoom = 7.5
func calcZoom(x: Double) -> Double {
let F1 = endZoom
let F0 = startZoom
let x0 = 0.0
let x1 = Double(sDuration)!
let B = log(F1 / F0) / (x1 - x0)
let A = F0 * exp(-B * x0)
let F = A * exp(B * x)
return F
}
func startAnimation() {
let Duration = Double(sDuration)!
timerA = Timer.scheduledTimer(withTimeInterval: step, repeats: true) { timer in
if currentTime >= Duration {
timer.invalidate()
}
scrollView.zoomScale = calcZoom(x: currentTime)
//Doesn't work as expected.
scrollView.contentOffset = CGPoint(x: startOffset.x + (endOffset.x - startOffset.x) * (currentTime / Duration),
y: startOffset.y + (endOffset.y - startOffset.y) * (currentTime / Duration))
currentTime += step
}
}