【问题标题】:UIScrollView contents not allowing user interactionUIScrollView 内容不允许用户交互
【发布时间】:2012-04-16 05:24:15
【问题描述】:

我有一个启用分页的 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,并将它们的交互设置为 yes,就像这样。

- (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];
}

【问题讨论】:

  • 你想要实现什么,你需要滚动视图滚动、网页视图滚动、分页、选择吗?请更具体。
  • 我需要滚动视图到页面并且用户能够在 webvies 上选择文本(它们是滚动视图的子视图)Webview 不需要滚动,只需能够选择其文本.
  • 我已经编辑了我的答案以反映您的要求,它似乎对我有用。
  • 注意:文档告诉你不要把 UIWebViews 放在 UIScrollViews 中。

标签: ios uiwebview uiscrollview


【解决方案1】:

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

[container setUserInteractionEnabled:YES];

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

yourWebView.scrollView.scrollEnabled = NO;

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

编辑:

我得到了这个工作!您还必须允许取消触摸:

[container setCanCancelContentTouches:YES];

【讨论】:

  • 我已经尝试过了,它破坏了作为滚动视图子级的 webviews 上的用户交互。
  • 我试过了,我不能再滚动了,即使 uiscrollview 的内容大小是 10,000px 并且框架设置为屏幕宽度/高度(768 x 1024)编辑:实际上,如果我按住几秒钟并尝试滑动。翻页非常困难。您是否遇到此问题?
  • 使用了滑动识别器并自己制作了分页动画。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多