链接文本视图以一起滚动

时间:2015-10-10 09:06:31

标签: ios uitextview swift2 xcode7

我有两个并排的文本视图。如何链接它们,以便如果其中一个向下滚动,则自动滚动与swift 2中的第一个相同。

1 个答案:

答案 0 :(得分:5)

设置viewController符合UITextViewDelegate,然后将textView委托设置为self,然后在scrollViewDidScroll同步contentOffSet

例如 GIF

enter image description here

代码

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
    }
}

}