Search code examples
iosswiftuiscrollviewuiscrollviewdelegate

Is there a 'willSet' & 'didSet' I can intercept for UIScrollview.contentOffset?


I have a situation where I need to gracefully move a frame when the contentOffset.y is a negative value.

The problem is I don't want to get the contentOffset call AFTER the content has already been dropped below 0.

Is there a willSet call where I can intercept it before the UIScrollView offsets its content?


Solution

  • You can add a willSet pretty easily in swift. Just subclass UIScrollView and only override that, like so. Obviously you'll wanna do something besides println, but I verified this definitely fires before the didScroll delegate does. I don't know that it will resolve your animation issue, but this is what you asked for.

    import UIKit
    
    class MyScrollView: UIScrollView {
    
        override var contentOffset: CGPoint { willSet { println("old value \(newValue)") } }
    
    }