简体   繁体   English

iOS-同时滚动两个UITextView

[英]iOS - Scrolling two UITextViews simultaneously

I've done some looking around but I haven't been able to find anything that clearly explains how I could simultaneously scroll two un-editable UITextViews. 我已经环顾四周,但找不到任何能够清楚解释如何同时滚动两个不可编辑的UITextView的内容。 I think I may need to use either scrollRangeToVisible , or setContentOffset , though I could not get either of them to work. 我想我可能需要使用scrollRangeToVisiblesetContentOffset ,尽管我无法使它们都起作用。

Does anyone have any examples/samples, or documentation regarding this that they could point me towards? 是否有人可以向我指出任何示例/样本或相关文档?

EDIT: To clarify, I would like to be able to scroll one UITextView, and have the changes as a result of the scrolling reflected on a second UITextView as well. 编辑:为澄清起见,我希望能够滚动一个UITextView,并且将由于滚动而产生的更改也反映在第二个UITextView上。

Thanks! 谢谢!

Use the UIScrollViewDelegate methods to get information about scroll actions of the first scroll view and then scroll the second programmatically like that: 使用UIScrollViewDelegate方法可获取有关第一个滚动视图的滚动动作的信息,然后像下面这样以编程方式滚动第二个:

- (void) scrollViewDidScroll:(UIScrollView *)view1 {
    scrollView2.contentOffset = view1.contentOffset;
}

Just continuing with previous answers, to give some more information, I generated this code: 只是继续前面的答案,以提供更多的信息,我生成了以下代码:

In the interface (.h): 在界面(.h)中:

#import <UIKit/UIKit.h>

@interface DoubleTextViewController : UIViewController <UITextViewDelegate>

@property (strong, nonatomic) IBOutlet UITextView *textView1;
@property (strong, nonatomic) IBOutlet UITextView *textView1;

@end

In your implementation (.m): 在您的实现(.m)中:

Use this viewDidLoad function after defining the corresponding properties and global variables. 定义相应的属性和全局变量后,请使用此viewDidLoad函数。

#import "DoubleTextViewController.h"

#define TEXT_VIEW_1_TAG 1001
#define TEXT_VIEW_2_TAG 1002

@interface DoubleTextViewController () {

    BOOL isScrolling;
}

@end

@implementation DoubleTextViewController

@synthesize textView1, textView2;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view..

    isScrolling = NO;

    [self.textView1 setTag:TEXT_VIEW_1_TAG];
    [self.textView2 setTag:TEXT_VIEW_2_TAG];

    [self.textView1 setDelegate:self];
    [self.textView2 setDelegate:self];
}

And add this function for simultaneous scroll. 并添加此功能以同时滚动。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if (isScrolling) {
        return;
    }


    isScrolling = YES;

    if (scrollView.tag == TEXT_VIEW_1_TAG) {

        [self.textView2 setContentOffset:scrollView.contentOffset animated:NO];

    } else if (scrollView.tag == TEXT_VIEW_2_TAG) {

        [self.textView1 setContentOffset:scrollView.contentOffset animated:NO];
    }

    isScrolling = NO;
}

As proposed by Hermann Klecker, the isScrolling variable stops scrolling loops and makes the user experience nicer. 正如Hermann Klecker提出的那样, isScrolling变量停止滚动循环,使用户体验更好。 Using the code proposed by Fabian Kreiser makes the scroll stops as soon as the user leaves the finger, making it strange. 使用Fabian Kreiser提出的代码,一旦用户离开手指,滚动就会停止,这很奇怪。

React in 反应在

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

and set the other's scrollView setContentVisible according to scrollView.contentOffset . 并根据scrollView.contentOffset设置另一个的scrollView setContentVisible

Just be aware that some methods of UIScrollView will invoke scrollViewDidScroll even if called programmatically. 请注意,即使以编程方式调用, UIScrollView某些方法也会调用scrollViewDidScroll This applies to scrollRangeToVisible and will end up in a loop unless you take action to prevent this loop. 这适用于scrollRangeToVisible,除非您采取措施防止该循环,否则它将最终循环。 I don't think that setContentOffset or setting scrollView2.contentOffset = CGPointMake(..,..) does call scrollViewDidScroll . 我不认为setContentOffset或设置scrollView2.contentOffset = CGPointMake(..,..)不会调用scrollViewDidScroll However, I would not sign this in blood. 但是,我不会在血液中签字。 Be prepared to see a loop and take actions to avoid it. 准备看到一个循环并采取措施避免该循环。 (such as boolean instance variable set before calling setContentOffset and re-set in scrollViewDidScroll followed by return; ) (例如在调用setContentOffset之前设置的布尔实例变量,然后在scrollViewDidScroll重新设置,然后return;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM