UIScrollView的内容不允许用户交互

7
我有一个启用分页的UIScrollView,如下所示:
container = [[UIScrollView alloc] initWithFrame:kScrollViewFrame];
[container setDelegate:self];
[container setShowsHorizontalScrollIndicator:YES];
[container setShowsVerticalScrollIndicator:NO];
[container setClipsToBounds:YES];
[container setPagingEnabled:YES];
[container setDecelerationRate:UIScrollViewDecelerationRateFast];
[container setBounces:NO];
[container setUserInteractionEnabled:NO];
[container setCanCancelContentTouches:NO];
[container setDelaysContentTouches:NO];

我在UIScrollView中添加了几个UIWebView,并将它们的交互启用设置为“是”。

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        self.frame = frame;
        self.userInteractionEnabled = YES;
    }
    return self;
}

这会破坏分页并影响UIScrollView上的所有触摸事件。如果我将用户交互设置为NO,分页可以正常工作,但我无法在UIWebView中突出显示文本。我尝试像下面这样子类化UIScrollView,但是仍然出现相同的情况。有什么想法吗?

- (id)initWithFrame:(CGRect)frame
{
    NSLog(@"init");
    return [super initWithFrame:frame];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesBegan");

    [[self nextResponder] touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesMoved");

    [[self nextResponder] touchesMoved:touches withEvent:event];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesEnded");
    [[self nextResponder] touchesEnded:touches withEvent:event];
}

你想要实现什么功能?你需要滚动视图滚动、网页视图滚动、分页或者选择吗?请具体说明。 - Bartosz Ciechanowski
我需要滚动视图进行分页,并且用户能够在Webview上选择文本(这些Webview是滚动视图的子视图)。Webview不需要滚动,只需要能够选择其文本。 - Oh Danny Boy
我已经编辑了我的答案以反映您的要求,它对我来说似乎是有效的。 - Bartosz Ciechanowski
2
注意:文档告诉你不要将UIWebViews放在UIScrollViews中。 - Glenn Maynard
1个回答

11

在容器上禁用用户交互将有效地禁用其在子视图上的交互。您应该启用它,以使触摸事件向下传播到视图层次结构中;

[container setUserInteractionEnabled:YES];

如果您想禁用UIWebView上的滚动,只需进入其UIScrollView中。

yourWebView.scrollView.scrollEnabled = NO;

我在我的设备上进行了测试,我可以在UIScrollView中“翻页”,也可以在UIWebView上选择文本。

编辑:

我已经解决了这个问题!你还需要允许触摸取消:

[container setCanCancelContentTouches:YES];

我已经尝试过这个方法,但它会破坏嵌套在滚动视图中的Web视图的用户交互。 - Oh Danny Boy
我尝试了这个方法,但现在我无法滚动屏幕,即使uiscrollview的内容大小为10,000px,框架已设置为屏幕宽度/高度(768 x 1024)。编辑:实际上,如果我按住几秒钟然后尝试滑动,它会起作用。翻页非常困难。你是否也遇到了这个问题? - Oh Danny Boy
使用了滑动识别器并手动实现了分页动画。 - Oh Danny Boy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接