【问题标题】:UIWebView's inside UIScrollView consuming touch eventsUIWebView 在 UIScrollView 内消耗触摸事件
【发布时间】:2011-07-05 18:13:24
【问题描述】:

我在 UIScrollView 中有多个自定义 UIWebView 作为子视图。我已经在 UIViewController 中为 UIWebView 实现了触摸方法,但是这些方法没有被调用。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { }
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { }
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { }

我在其他地方读到过,它说 UIScrollView 将使用这些事件。这是有道理的,但是 UIWebView 是否应该在 UIScrollView 本身之前不接收触摸事件?可能发生的情况是 UIWebBrowserView(UIWebView 的子视图)正在使用这些事件。然而,这个类不能被子类化,所以我不能将事件沿着 superview 链发送到 UIScrollView。我在 Apple iOS 文档上阅读过,不应该将 UIWebView 放在 UIScrollView 中,但出于我的目的,我需要这样做。

所以我的问题是:我的自定义 UIWebView(即 UIScrollView 的子视图)如何接收触摸事件?

我的控制器和视图设置如下:

HomeViewController.h /.m /.xib
HomeView.h /.m (is a UIScrollView)
DashboardViewController.h /.m /.xib (custom UIWebView's)

HomeViewController 从包含自定义视图的 Nib 文件加载:HomeView。 DashboardViewController 从 Nib 文件加载。它包含一些标签、滑块等(请注意,这里没有自定义视图 - 我这里没有继承 UIView)。

HomeViewController 动态加载多个 DashboardViewController 并将其视图添加到 HomeView。

DashboardViewController *dashboardViewController = [[DashboardViewController alloc] initWithNibName:@"DashboardViewController" bundle:[NSBundle mainBundle];
[homeView addSubview:dashboardViewController.view];

【问题讨论】:

    标签: objective-c ios cocoa-touch uiview uiscrollview


    【解决方案1】:

    您可能正在寻找canCancelContentTouches

    scrollView.canCancelContentTouches = NO;
    

    【讨论】:

      【解决方案2】:

      用户交互是标准启用的。

      您是否将视图委托连接到文件所有者? 您是否将视图绑定到控制器?并确保将相应的操作连接到正确的方法。 (CTRL 单击视图并将加号拖到 Class.h 文件中的相应方法上。

      @interface MyScrollView : UIViewController {
          UIView *myFirstView;
          UIView *mySecondView;
          UIView *myThirdView;
      }
      
      @property (nonatomic, retain) IBOutlet UIView *myView;
      
      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
      - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
      - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
      
      @end
      

      【讨论】:

      • 是的,我的意见委托已连接到文件的所有者。你是什​​么意思将我的视图绑定到控制器?此外,我已将 UIScrollView 子类连接为 Controller 的主视图。查看我更新的帖子;我添加了一些可能有帮助的更多信息。谢谢。
      • 此解决方案不起作用,因为 UIWebBrowserView(不是 UIWebView)似乎会接收触摸事件。然而,这个类不能被子类化来改变功能。
      【解决方案3】:

      您是否为所有子视图设置了userInteractionEnabled = YES

      【讨论】:

      • userInteractionEnabled 默认启用。
      • 糟糕。以为默认是NO。滚动视图上的 contentSize 可能吗?
      【解决方案4】:

      UIViewController 在 UIView 之前获取 touches* 消息。默认情况下,UIViewController 不会将消息传递给 UIView(通过调用 [super touchesBegan])。因此,如果您的 UIView 有控制器,则需要移动或转发 touches* 功能。

      我个人将各种 touches* 方法放在视图子类上,而不是视图控制器子类上。

      【讨论】:

      • 这里的问题是我没有将 UIView 子类化为 UIScrollView 中的子视图。我只是使用从 Nib 生成的视图。查看我更新的帖子;我添加了一些可能有帮助的更多信息。谢谢。
      【解决方案5】:

      你可以试试这个,当你依次为 UIScrollView 调用 touches 方法时,你可以在子视图中从 scrollview 调用相同的 touches 方法。

      【讨论】:

        猜你喜欢
        • 2018-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-10
        相关资源
        最近更新 更多