Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would take a slightly different tack in trying to keep two text views scroll position synchronized. I would make the overall view's <code>UIViewController</code> a <code>UIScrollViewDelegate</code>. I would implement a <code>scrollViewDidScroll:</code> 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. </p> <p>Additional methods in the <code>UIScrollViewDelegate</code> could be implemented to keep synchronization during user scrolling, depending upon how I wanted the scrolling text views to act.</p> <p>In response to comments:</p> <p>It sounds like the percentage calculation would work best in this situation. Each scroll view has <code>contentSize</code> and <code>contentOffset</code> properties. The other view's offset can be calculated with <code>(thisView.contentOffset.x/thisView.contentSize.height) * otherView.contentSize.height</code>. </p> <p>I was thinking the flag may be necessary if calling setContentOffset:animated: on the other scroll would trigger another call to the delegate's <code>scrollViewDidScroll:</code> 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 <code>scrollViewDidScroll</code>.</p> <p><code>scrollViewDidScroll:</code> 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload