0

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
    }
}
TylerP
  • 9,600
  • 4
  • 39
  • 43
MG1
  • 1
  • 1
  • 2
    It's a little difficult to tell what you're trying to do, and what's not working. Post a minimal example with just enough code so we can copy/paste and run it. – DonMag Oct 29 '22 at 12:46

0 Answers0