Can I animate the UIScrollView contentOffset property via its layer?

Viewed 21108

I want to zoom and scroll a UIScrollView with a CGPathRef. Because of that I assume I have to animate the UIScrollView's layer property? But which property would I animate that would make it equivalent to doing a UIView animation and setting its contentOffset property and zoomScale ?

These are not properties of a CALayer.

Any ideas as to how I would approach this? Again, just want to move the scrollview to a certain contentOffset and zoomScale, but not necessarily linearly from point A to point B, zoom A to zoom B, respectively.

I was thinking a CAKeyFrameAnimation with a CGPathRef, but I don't know which properties to animate.

3 Answers

Swift 4.2

This example is base on pt2ph8's obj-c answer.

https://stackoverflow.com/a/8741283/6113158

var scrollView = UIScrollView()

func animateScrollView(duration: Double, to newBounds: CGRect) {
    let animation = CABasicAnimation(keyPath: "bounds")
    animation.duration = duration
    animation.fromValue = scrollView.bounds
    animation.toValue = newBounds

    animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeOut)

    scrollView.layer.add(animation, forKey: nil)

    scrollView.bounds = newBounds
}
Related