iOS - 同时滚动两个 UITextView
我环顾四周,但找不到任何可以清楚解释如何同时滚动两个不可编辑的 UITextView 的内容。我想我可能需要使用 scrollRangeToVisible
或 setContentOffset
,尽管我无法让它们中的任何一个工作。
有谁有任何示例/样本或与此相关的文档可以向我指出吗?
编辑:为了澄清,我希望能够滚动一个 UITextView,并将滚动结果的更改也反映在第二个 UITextView 上。
谢谢!
I've done some looking around but I haven't been able to find anything that clearly explains how I could simultaneously scroll two un-editable UITextViews. I think I may need to use either scrollRangeToVisible
, or setContentOffset
, though I could not get either of them to work.
Does anyone have any examples/samples, or documentation regarding this that they could point me towards?
EDIT: To clarify, I would like to be able to scroll one UITextView, and have the changes as a result of the scrolling reflected on a second UITextView as well.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 UIScrollViewDelegate 方法获取有关第一个滚动视图的滚动操作的信息,然后以编程方式滚动第二个滚动视图,如下所示:
Use the UIScrollViewDelegate methods to get information about scroll actions of the first scroll view and then scroll the second programmatically like that:
根据
scrollView.contentOffset
反应进去并设置对方的scrollView
setContentVisible
。请注意,即使以编程方式调用,
UIScrollView
的某些方法也会调用scrollViewDidScroll
。这适用于scrollRangeToVisible,并且将最终陷入循环,除非您采取措施阻止此循环。我认为setContentOffset
或设置scrollView2.contentOffset = CGPointMake(..,..)
不会调用scrollViewDidScroll
。然而,我不会用血来签署这个协议。准备好发现循环并采取措施避免它。 (例如在调用setContentOffset
之前设置的布尔实例变量,并在scrollViewDidScroll
中重新设置,然后return;
)React in
and set the other's scrollView
setContentVisible
according toscrollView.contentOffset
.Just be aware that some methods of
UIScrollView
will invokescrollViewDidScroll
even if called programmatically. This applies to scrollRangeToVisible and will end up in a loop unless you take action to prevent this loop. I don't think thatsetContentOffset
or settingscrollView2.contentOffset = CGPointMake(..,..)
does callscrollViewDidScroll
. However, I would not sign this in blood. Be prepared to see a loop and take actions to avoid it. (such as boolean instance variable set before callingsetContentOffset
and re-set inscrollViewDidScroll
followed byreturn;
)继续之前的答案,为了提供更多信息,我生成了以下代码:
在接口 (.h) 中:
在您的实现 (.m) 中:
在定义相应的属性和之后使用此
viewDidLoad
函数全局变量。并添加此功能以实现同时滚动。
正如 Hermann Klecker 所建议的,isScrolling 变量会停止滚动循环并使用户体验更好。使用 Fabian Kreiser 提出的代码,一旦用户离开手指,滚动就会停止,这很奇怪。
Just continuing with previous answers, to give some more information, I generated this code:
In the interface (.h):
In your implementation (.m):
Use this
viewDidLoad
function after defining the corresponding properties and global variables.And add this function for simultaneous scroll.
As proposed by Hermann Klecker, the
isScrolling
variable stops scrolling loops and makes the user experience nicer. Using the code proposed by Fabian Kreiser makes the scroll stops as soon as the user leaves the finger, making it strange.