简体   繁体   English

我可以拦截UIScrollview.contentOffset的'willSet'和'didSet'吗?

[英]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. 我遇到一种情况,当contentOffset.y为负值时,我需要优雅地移动框架。

The problem is I don't want to get the contentOffset call AFTER the content has already been dropped below 0. 问题是我不希望在内容已经降至0以下之后再调用contentOffset

Is there a willSet call where I can intercept it before the UIScrollView offsets its content? UIScrollView偏移其内容之前,是否有willSet调用可在其中进行拦截?

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 . UIScrollView的子类中,只需重写contentOffsetwillSet It seems that is called before layoutSubviews() . 似乎在layoutSubviews()之前调用了它。

You can add a willSet pretty easily in swift. 您可以快速轻松地添加willSet。 Just subclass UIScrollView and only override that, like so. 只是子类UIScrollView,并且仅重写它,就像这样。 Obviously you'll wanna do something besides println, but I verified this definitely fires before the didScroll delegate does. 显然,您除了println之外还想做些其他事情,但是我确认在didScroll委托之前确实可以执行此操作。 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 . 您可以使用委托方法scrollViewDidScrollscrollViewWillBeginDecelerating并拦截contentOffset.y I hope this help you. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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