0

I'm using KVO to observe the the contentOffset property of a UIScrollView. When the user is pulling the scrollView down and the contentOffset.y == -50.0, I want the scroll view to release as if the user took their finger off the screen. Is there anyway to do this?

4

1 回答 1

0

是的,您可以将其偏移量限制为 -50.0。

- (void) observeValueForKeyPath:(NSString*)keyPath 
                       ofObject:(id)object
                         change:(NSDictionary*)change
                        context:(void*)context
{
    //if this is not the only KVO then you should first perform some checks
    //if the object and keypath are correct

    //otherwise you can omit the check or modify, if your UIScrollView is subclassed

    if ([object isKindOfClass:[UIScrollView class]])
    {
        UIScrollView *scrl = (UIScrollView *)object;

        CGPoint offset = scrl.contentOffset;

        if (offset.y < -50.0f)
        {
            offset.y = -50.0f;
            scrl.contentOffset = offset;
        }
    }
}

您可能需要考虑在scrollView委托方法(和其他方法)中移动此代码。KVO 是一个很棒的工具,但对于快速更改值didScroll:而言,它可能过于昂贵。

于 2013-04-26T16:33:21.843 回答