链接文本视图一起滚动

Linking text views to scroll together

我有两个并排的文本视图。我怎样才能 link 它们,以便如果一个向下滚动,另一个自动滚动与 swift 中的第一个相同 2.

设置你的 viewController 符合 UITextViewDelegate,然后将 textView delegate 设置为 self,然后在 scrollViewDidScroll 同步两个 contentOffSet

例如 动图

代码

class ViewController: UIViewController,UITextViewDelegate {

@IBOutlet weak var textview2: UITextView!
@IBOutlet weak var textview1: UITextView!
override func viewDidLoad() {
    super.viewDidLoad()
    textview1.delegate  = self
    textview2.delegate  = self
    // Do any additional setup after loading the view, typically from a nib.
}
func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView == textview1{
        textview2.contentOffset = textview1.contentOffset
    }else{
        textview1.contentOffset = textview2.contentOffset
    }
}

}