暂停UIWebView中的JavaScript执行

use*_*416 10 javascript animation objective-c uiwebview ios

我将多个UIWebView并排放入一个UIScrollView.每个都UIWebView包含一个显示本地HTML文件的"Web浏览器"视图.不幸的是,我不知道如何在后台加载webview,同时阻止或阻止JavaScript函数执行包含在HTML文件中.我的意思是"在后台"我观看前几个面板,同时剩下的面板只是懒得加载.我看到不同的应用程序(如pugpig)这样做 - 它延迟加载HTML页面并停止JavaScript动画 - 它可以某种方式阻止JavaScript动画我在我的HTML页面中切换选项卡之间经常使用的整个淡入淡出效果我加载面板继续淡入淡出动画.

所以,问题是:

如何暂停JavaScript动画并使用Objective-C代码防止内存问题?如何在显示面板后继续淡入淡出动画?

Wil*_*Niu 4

您是否尝试过注入观察 、 或 的 JavaScript(即- ( NSString * ) stringByEvaluatingJavaScriptFromString: (NSString *)script)?这篇文章为此提出了一些解决方案。onbluronfocuswindowpageshowpagehide

所以,你可以尝试类似的事情

$(window).blur(function(){
  // when the web view loses focus
});
$(window).focus(function(){
  // when the web view gains focus
});
Run Code Online (Sandbox Code Playgroud)

如果失败,您可以尝试pagehide以下pageshow事件:

function pageShown(evt)
{
    if (evt.persisted)
        // The page was just restored from the Page Cache
    else
        // The initial load. This is the same as the page load event
}

function pageHidden(evt)
{
    if (evt.persisted)
        // The page was suspended and placed into the Page Cache
    else
        // This is the same as the page unload event
}

window.addEventListener("pageshow", pageShown, false);
window.addEventListener("pagehide", pageHidden, false);
Run Code Online (Sandbox Code Playgroud)