admin管理员组文章数量:1322511
I placed multiple UIWebView
s side by side into a UIScrollView
. Every UIWebView
includes a "web browser" view that displays a local HTML file. Unfortunately I have no idea how to load the webviews in the background and at the same time to prevent or stop the JavaScript functions from executing that are included into the HTML files. I mean by "in the background" as I watch the first few panels and in the meantime the remainder panels just lazy load silently. I saw different applications (like pugpig) do this - it lazy loads the HTML pages and stops the JavaScript animations - it can somehow stop the JavaScript to animate the whole fade effect that I use frequently in my HTML pages between tab switches and at the moment I load the panel it continues the fade animation.
So, the question is:
How can I pause a JavaScript animation and prevent memory issues using Objective-C code and how can I continue the fade animation once I display the panel?
I placed multiple UIWebView
s side by side into a UIScrollView
. Every UIWebView
includes a "web browser" view that displays a local HTML file. Unfortunately I have no idea how to load the webviews in the background and at the same time to prevent or stop the JavaScript functions from executing that are included into the HTML files. I mean by "in the background" as I watch the first few panels and in the meantime the remainder panels just lazy load silently. I saw different applications (like pugpig) do this - it lazy loads the HTML pages and stops the JavaScript animations - it can somehow stop the JavaScript to animate the whole fade effect that I use frequently in my HTML pages between tab switches and at the moment I load the panel it continues the fade animation.
So, the question is:
How can I pause a JavaScript animation and prevent memory issues using Objective-C code and how can I continue the fade animation once I display the panel?
Share Improve this question edited Sep 22, 2012 at 9:26 rid 63.6k31 gold badges157 silver badges197 bronze badges asked Jan 20, 2012 at 19:42 Heaven 7Heaven 7 1231 silver badge7 bronze badges3 Answers
Reset to default 4 +75Have you tried injecting JavaScript (i.e. - (NSString*) stringByEvaluatingJavaScriptFromString: (NSString *)script) that observes onblur
and onfocus
of the window
, or pageshow
and pagehide
? This post proposes a couple of solutions for that.
So, you could try something like
$(window).blur(function(){
// when the web view loses focus
});
$(window).focus(function(){
// when the web view gains focus
});
If that fails, you could try pagehide
and pageshow
events:
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);
I guess you can control the animation javascripts that runs in the HTML (you said the page is local). What would I do is to wrap the animation in a JS function that would get called from Cocoa (there's tons of references for that on StackOverFlow e.g. - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script).
Then I would create a stack that has a record for each one of the HTML pages you want to lazy load. On every UIScrollView scrollViewDidScroll: I would check if a scroll in the size of a whole webview size has performed if so I would pop that record from the stack and call the Javascript startAnimation function we mentioned before on the next webview.
If you need double-sided munication (from javascript to cocoa) for example to indicate that the animation was pleted you can use JSBridge. (How do I call an Objective-C method from JavaScript on UIWebView?)
Maybe you can try evaluating (using stringByEvaluatingJavaScriptFromString
) a purposely-faulty javascript code fragment and cause the js- interpreter to choke up?
<script> var j = // note: missing value, error!
</script>
本文标签: objective cPause JavaScript execution in UIWebViewStack Overflow
版权声明:本文标题:objective c - Pause JavaScript execution in UIWebView - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742115577a2421452.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论