简体   繁体   中英

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?

It seems, this works:

class MyScrollView: UIScrollView {
    override var contentOffset:CGPoint {
        willSet {
            println("newOffset: \(newValue)")
        }
    }
}

In your subclass of UIScrollView , just override the willSet of contentOffset . It seems that is called before layoutSubviews() .

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)") } }

}

You can use the delegate methods scrollViewDidScroll or scrollViewWillBeginDecelerating and intercept the contentOffset.y . I hope this help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM