我有两个并排的文本视图。如何链接它们,以便如果其中一个向下滚动,则自动滚动与swift 2中的第一个相同。
答案 0 :(得分:5)
设置viewController符合UITextViewDelegate
,然后将textView委托设置为self,然后在scrollViewDidScroll
同步contentOffSet
例如 GIF
代码
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
}
}
}