【问题标题】:Make an touchesbegan event to uiwebview对 uiwebview 进行 touchesbegan 事件
【发布时间】:2013-12-16 21:02:11
【问题描述】:

我希望你能帮助我,首先对不起我的英语不好。 我想在 iOS 的 uiwebview 上点击一个指定点 (x/y)。我已经尝试过

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

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [webview touchesBegan:touches withEvent:event]; }

但我还没有工作。我认为 uiwebview 是 uiview 的子视图。

你能帮帮我吗?

来自德国的问候。

【问题讨论】:

    标签: ios xcode uiview uiwebview touchesbegan


    【解决方案1】:

    只需像这样调用一个javascript函数:

    [myWebView stringByEvaluatingJavaScriptFromString:"/*JS Code goes here*/"];
    

    而不是/*JS Code goes here*/ 通过单击所需的 x,y 位置,键入 java 脚本代码以执行您想要执行的操作。我假设你可以控制网页内容,如果不是请评论,我会添加更复杂的解决方案。

    如果您要调用在包含在页面内容中的 iframe 中定义的函数,您需要定义两个函数:

    1- iframe 页面内的 JS 函数,显示为 iframe 标签的源,此函数为 iFrameFunc()

    2- 父 HTML 页面中的 JS 函数命名为 parentFunc()

    3- parentFunc 应该通过 id 或类选择器获取 iframe 的句柄并调用 iframeFunc()

    4- Obj-c 会将 [UIWebview stringByEvaluatingJavaScriptFromString] 调用到 parentFunc

    【讨论】:

    • 抱歉,没有意见,因为该位置在 iframe 中,我无法更改这些。但是谢谢!
    • 你是否试图将焦点放在某个元素上?
    • 什么意思?通过js?
    • 从 iPhone UIWebView 你可以在你的页面中调用 JavaScript 函数或者直接使用我在上面答案中的方法执行一些 JavaScript 代码。
    • 是的,但是由于跨站点脚本,我无法单击 iframe 加载的对象
    【解决方案2】:

    我对@9​​87654321@ 进行了子类化,并且只是在 2 级深的子视图中“吸食”到它的手势识别器上(你可以递归地进行,但这对于 iOS6-7 来说已经足够了)。 然后你可以对触摸位置和手势识别器的状态做任何你想做的事情。

    for (UIView* view in self.subviews) {
        for (UIGestureRecognizer* recognizer in view.gestureRecognizers) {
            [recognizer addTarget:self action:@selector(touchEvent:)];
        }
        for (UIView* sview in view.subviews) {
            for (UIGestureRecognizer* recognizer in sview.gestureRecognizers) {
                [recognizer addTarget:self action:@selector(touchEvent:)];
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多