【问题标题】:Programmatically cancel user scrolling a UIScrollView以编程方式取消用户滚动 UIScrollView
【发布时间】:2013-04-26 16:07:06
【问题描述】:

我使用KVO 来观察UIScrollViewcontentOffset 属性。当用户拉下scrollViewcontentOffset.y == -50.0 时,我希望释放滚动视图,就像用户将手指从屏幕上移开一样。有没有办法做到这一点?

【问题讨论】:

    标签: iphone ios uiscrollview uiresponder


    【解决方案1】:

    是的,您可以将其偏移量限制为 -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 是一个很棒的工具,但对于快速更改值而言,它可能过于昂贵

    【讨论】:

    • 感谢您的回复,但不幸的是,设置滚动视图的 contentOffset 属性不会使滚动视图像用户放开它一样。我只是在那里尝试过。
    • 嗯,也许我理解错了。因此,您希望 scrollView 继续滚动,就像用户抬起手指一样?在这种情况下,当y 达到-50.0 时,我会尝试将touchesEnabled 设置为NO,但您还必须实现一个委托方法didEndScrollingAnimation,您可以在其中重新启用scrollView 的触摸:我' d 明确地将所有与行为相关的代码移至 delegate 方法,并在这种特殊情况下放弃 KVO。
    猜你喜欢
    • 2011-01-15
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    相关资源
    最近更新 更多