iOS SDK:一个ScrollView中有两个TextView?
是否可以将两个TextView放在一个ScrollView中,这样只有ScrollView滚动,而嵌入的两个TextView不能独立滚动? (但是如果 ScrollView 滚动,TextView 也应该滚动,以便显示其余文本。)
谢谢。此致,丹尼尔
编辑:
当前代码 http://www.muehlbachler.org/spt.zip
声明我需要什么以及该应用程序到目前为止的功能: 其中一部分(刚刚需要的部分)是一个简单的端口检查器,它检查一系列端口是否打开或被主机拒绝。为此,表单下方有两个主要部分,称为“开放端口”和“封闭端口”。每个主要部分下有两个文本视图,其中显示端口(打开或关闭)。我制作了两个文本视图,因为如果您想查看所有打开/关闭的端口,这是一个更好的体验,因为这样您就不能经常滚动。 现在的问题是每个文本视图都可以独立滚动,但这有点令人讨厌,因为我只想将打开/关闭的端口文本视图一起滚动。事实上,两个文本视图必须组合在一起,以便它们只能一起滚动。 举个例子:假设您有 100 个关闭的端口,它们现在显示为两个文本视图 (50 : 50)。现在您想要快速查看所有关闭的端口号,并且(现在)您必须滚动两次,因为您必须分别滚动 textview1 和 textview2。现在出现了期望的行为:您只想滚动一次,如果您滚动那一次,您希望看到所有关闭的端口(因为两个单独的文本视图都滚动)。 我的想法是,我只是在文本视图之前放置一个滚动视图,然后我可以将它们滚动在一起,但这不起作用...:(
编辑:
添加了图像。由于我的声誉,我不允许添加图像,所以我上传了它:http://www.muehlbachler.org/spt.jpg
Is it possible to put two TextViews in one ScrollView, so that only the ScrollView scrolls and the two embedded TextViews can't scroll independently? (But if the ScrollView scrolls the TextViews should scroll either so that the rest of the text is shown.)
Thx. Regards, Daniel
Edit:
Current code http://www.muehlbachler.org/spt.zip
Declaration of what I need and what the app does so far:
One part (the just needed one) is a simple port checker which checks a range of ports if they are open or rejected by a host. For that there are two main sections below the form called "open ports" and "closed ports". Under each main section are two textviews where the ports are displayed (either they are open or closed). I made two textviews because it is a better experience if you want to see all open/closed ports because then you must not scroll so often.
The problem now is that each textview can scroll independently but that's abit nasty because I just want to scroll the open/closed ports textviews together. In fact, two of the textviews must be grouped together so that they can only scroll together.
An example: imagine you have 100 closed ports and they are now displayed divided into two textviews (50 : 50). Now you want to take a quick look at all closed port numbers and (now) you have to scroll two times because you have to scroll textview1 and textview2 separately. Now the wished behaviour comes in: you just want to scroll one time and if you scroll that time you want to see all closed ports (because the two separate textviews are scrolled either).
My idea was that I simply put a scrollview before the textviews and then I can scroll them together, but that doesn't work... :(
Edit:
Image added. I'm not allowed to add immages due to my reputation, so I uploaded it: http://www.muehlbachler.org/spt.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会采取稍微不同的策略来尝试保持两个文本视图滚动位置同步。我会将整个视图的
UIViewController
设为UIScrollViewDelegate
。我将实现一个scrollViewDidScroll:
方法,该方法会询问刚刚滚动的文本视图滚动到的位置,然后告诉其他文本视图滚动到相同的位置。为了防止此方法不断重复的循环,我设置了一个标志以在发送滚动消息之前进行检查。如果保证两个文本视图的大小相同,则这非常容易,但如果它们大小不同,则可能需要更多工作。也许滚动位置将按百分比计算。也许滚动视图需要了解内容,并滚动到特定标题。可以实现 UIScrollViewDelegate 中的其他方法来在用户滚动期间保持同步,具体取决于我希望滚动文本视图的行为方式。
回应评论:
听起来百分比计算在这种情况下效果最好。每个滚动视图都有
contentSize
和contentOffset
属性。另一个视图的偏移量可以使用(thisView.contentOffset.x/thisView.contentSize.height) * otherView.contentSize.height
计算。我认为如果在另一个滚动上调用 setContentOffset:animated: 会触发对委托的
scrollViewDidScroll:
方法的另一次调用,则可能需要该标志。检查标志可以停止循环。如果设置了该标志,请不要告诉其他滚动视图移动,因为这是一个反动的scrollViewDidScroll
。scrollViewDidScroll:
每次需要重绘滚动视图时都会调用。因此,在一次用户交互中,该方法可能会被调用数百次。仅此一项就能够保持两个滚动视图同步。I would take a slightly different tack in trying to keep two text views scroll position synchronized. I would make the overall view's
UIViewController
aUIScrollViewDelegate
. I would implement ascrollViewDidScroll:
method that would ask the just scrolled text view where it scrolled to and then tell the other text view to scroll to the same position. To prevent a loop where this method keeps repeating I'd set a flag to check before sending the scroll message. This is very easy if both text views are guaranteed to be the same size, but may need more work if they are different sizes. Maybe the scroll position would be calculated on percentages. Maybe the scroll views need to know about content, and scroll to specific headings.Additional methods in the
UIScrollViewDelegate
could be implemented to keep synchronization during user scrolling, depending upon how I wanted the scrolling text views to act.In response to comments:
It sounds like the percentage calculation would work best in this situation. Each scroll view has
contentSize
andcontentOffset
properties. The other view's offset can be calculated with(thisView.contentOffset.x/thisView.contentSize.height) * otherView.contentSize.height
.I was thinking the flag may be necessary if calling setContentOffset:animated: on the other scroll would trigger another call to the delegate's
scrollViewDidScroll:
method. Checking the flag could stop the loop. If the flag is set, don't tell the other scroll view to move, because this is a reactionaryscrollViewDidScroll
.scrollViewDidScroll:
is called every time the scroll view needs to be redrawn. So in one user interaction this method may be called hundreds of times. This alone would be able to keep the two scroll views in sync.为什么在滚动视图中需要两个文本视图,那么为什么不只使用嵌入了滚动视图的文本视图来查看其余文本,只需滚动文本视图即可。
why need two textView inside scrollview then why not use only textview it has scrollview embeded to see the rest of text just scroll the textview.