【问题标题】:(iphone) uiscrollview not scrolling even though it receives touchesBegan/moved/ended event(iphone) uiscrollview 不滚动,即使它接收到 touchesBegan/moved/ended 事件
【发布时间】:2011-02-10 11:42:07
【问题描述】:

我已将 scrollView.userInteractionEnabled 设置为 NO,以便 superview 捕获触摸事件。

在适当的时候,从我的一个自定义 view 类中调用

scrollView.userInteractionEnabled = YES;
[scrollView touchesBegan: touches withEvent: event];
or moved/ended similarly.

我可以看到我的 scrollView 正在通过我的调试输出获取触摸事件消息。
但我没有看到它滚动这怎么可能?

contentSize 足够大,scrollEnabled 为 YES。
(当我在启动时设置 scrollView.userInteractionEnabled = YES 时,scrollView 滚动正常)

例如,我的 UIScrollView 子类有

- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
    SYSLOG(LOG_DEBUG, "MyScrollView touchesBegan");
    [self setUserInteractionEnabled: YES];
    [self setScrollEnabled: YES];
    [super touchesBegan: touches withEvent: event];
}
  • 编辑

在视图类或视图控制器类中重载 touchesBegan 可能会有所不同?

【问题讨论】:

    标签: iphone touch scrollview


    【解决方案1】:

    看起来你不能只是传递触摸并让它响应它们:(

    您必须使用 [UIScrollView setContentOffset:animated:] 方法自行移动滚动视图。


    拦截触摸的更好方法可能是在滚动视图前面放置一个视图 - 抓取您想要监听的任何触摸,然后将其传递给链中的下一个响应者。

    您将创建一个覆盖触摸处理方法的 UIView 子类,例如:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        if (scrollViewShouldGetThisTouch)
            [self.nextResponder touchesBegan:touches withEvent:event];
    
    }
    

    只需让该视图与您的滚动视图具有相同的框架,但在 ui 中位于其前面(当然是透明的!)。

    您可以继承 UIScrollView - 尽管如果 Apple 更改 UIScrollView 的实现,这可能会导致奇怪的错误。我会选择其他选项!

    【讨论】:

    • 哦..我会尽快试试这个,谢谢。我认为触摸是在最深的节点(在视图层次结构中)和向上处理的(因此来自子视图的下一个响应者将是父视图)。我想我弄错了。
    • 只是为了确定,您的意思是,将透明视图作为滚动视图的子视图,对吗?
    • 我在scrollview前面放了一个空视图(作为兄弟视图),正如担心的那样,nextResponder不是scrollView而是空视图的superview
    • 好的,在这种情况下尝试[super touchesBegan:touches withEvent:event] 看看是否有帮助? (我永远无法让响应链的东西第一次工作!)
    • 它不起作用,我认为调用 touchesBegan 无论如何都不会启动滚动,但是在滚动视图前面放置一个视图可以帮助拦截触摸
    猜你喜欢
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2012-05-03
    • 2012-03-20
    相关资源
    最近更新 更多