iOS SDK: Two TextViews in one ScrollView?

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

Upvotes: 0

Views: 1256

Answers (2)

Mr. Berna
Mr. Berna

Reputation: 10645

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 a UIScrollViewDelegate. I would implement a scrollViewDidScroll: 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 and contentOffset 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 reactionary scrollViewDidScroll.

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.

Upvotes: 1

Rahul Vyas
Rahul Vyas

Reputation: 28720

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.

Upvotes: 0

Related Questions