Can I animate the UIScrollView contentOffset property via its layer?

后端 未结 3 1093
梦如初夏
梦如初夏 2021-01-30 14:37

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 th

3条回答
  •  Happy的楠姐
    2021-01-30 14:44

    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
    }
    

提交回复
热议问题