如何同步 NSScrollView 和 WebView 的滚动?
我的应用程序有两个主要视图:NSScrollView 中的 NSTextView 子类和 WebView。 WebView 显示的内容取决于用户在文本视图中输入的内容 - 所以我希望当用户滚动文本视图或 Web 视图时,其他视图也按比例滚动。
我发现 这篇文章提到了如何使用 2 个滚动视图来做到这一点。我的问题是 WebKit 似乎没有在任何地方使用正常的滚动视图。
我应该如何实施这个?我缺少什么?
My app has two main views: a NSTextView subclass within a NSScrollView and a WebView. What the WebView displays is dependent on what the user enters into the text view - so I would like when the user scrolls either the text view or the web view the other scrolls proportionately to it.
I found this article which mentions how to do it with 2 scroll views. My problem is that WebKit doesn't seem to use normal Scroll views anywhere.
How should I implement this? What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是一个需要完美解决的小问题,因为很难知道文本视图中正在编辑的文本量是否与 Web 视图中类似的滚动量相对应。
但是,为了回答您有关 WebView 中滚动视图的问题,使用了它们,但据我所知,还没有广泛记录。您必须利用这样一个事实:您可以使用公共 API,通过向适当的 WebView 子视图询问其“enendingScrollView”来获取正在使用的滚动视图。像这样的东西对我来说适用于 WebView,我知道只有一个框架:
This is not a trivial problem to solve perfectly, as it's difficult to know whether the amount of text being edited in your text view corresponds to a similar amount of scrolling in the web view.
However, to answer your question about scroll views in WebView, they are used but as far as I know not documented extensively. You have to take advantage of the fact that you can obtain the scrollview being used, using public API, by asking the appropriate WebView subview for its "enclosingScrollView". Something like this works for me in a WebView where I know that there is only one frame:
如果它们按比例滚动,最简单的解决方案可能是覆盖
UIScrollview
上的触摸事件,并使用 (js) 在UIWebView
上实现stringByEvaluatingJavaScriptFromString:
)window.scroll(x,y)
。然而,从 webView 滚动到 textview 将需要更多的工作。 webView 处理触摸事件,因此您需要创建一个顶级
UIView
或UIScroll
视图来捕获触摸并将其发送到用于所有触摸事件的scrollView
和webView
(通过javascript),或使用手势识别器执行相同操作。
If they are scrolling proportionately, probably the simplest solution would be to override touches events on your
UIScrollview
and implimentstringByEvaluatingJavaScriptFromString:
on theUIWebView
with (js)window.scroll(x,y)
. However, scrolling from the webView to the textview will require more work. The webView eats touches events, so you would need tocreate a top level
UIView
orUIScroll
view which captures the touches and sends them on to both thescrollView
and thewebView
(via javascript) for ALL touches events, oruse a gesture recognizer to do the same.