以编程方式取消用户滚动UIScrollView

时间:2013-04-26 16:07:07

标签: iphone ios uiscrollview uiresponder

我正在使用KVO来观察contentOffset的{​​{1}}属性。当用户向下拉UIScrollViewscrollView时,我希望滚动视图释放,就像用户将手指从屏幕上移开一样。反正有没有这样做?

1 个答案:

答案 0 :(得分: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委托方法(didScroll:和其他方法中移动此代码.KVO是一个很棒的工具,但对于快速更改的值,它可能太昂贵