iOS - 同时滚动两个 UITextView

发布于 2024-12-02 06:46:11 字数 262 浏览 3 评论 0原文

我环顾四周,但找不到任何可以清楚解释如何同时滚动两个不可编辑的 UITextView 的内容。我想我可能需要使用 scrollRangeToVisiblesetContentOffset,尽管我无法让它们中的任何一个工作。

有谁有任何示例/样本或与此相关的文档可以向我指出吗?

编辑:为了澄清,我希望能够滚动一个 UITextView,并将滚动结果的更改也反映在第二个 UITextView 上。

谢谢!

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. I think I may need to use either scrollRangeToVisible, or setContentOffset, though I could not get either of them to work.

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.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑着哭最痛 2024-12-09 06:46:11

使用 UIScrollViewDelegate 方法获取有关第一个滚动视图的滚动操作的信息,然后以编程方式滚动第二个滚动视图,如下所示:

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

Use the UIScrollViewDelegate methods to get information about scroll actions of the first scroll view and then scroll the second programmatically like that:

- (void) scrollViewDidScroll:(UIScrollView *)view1 {
    scrollView2.contentOffset = view1.contentOffset;
}
jJeQQOZ5 2024-12-09 06:46:11

根据scrollView.contentOffset反应进去

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

并设置对方的scrollViewsetContentVisible

请注意,即使以编程方式调用,UIScrollView 的某些方法也会调用 scrollViewDidScroll。这适用于scrollRangeToVisible,并且将最终陷入循环,除非您采取措施阻止此循环。我认为 setContentOffset 或设置 scrollView2.contentOffset = CGPointMake(..,..) 不会调用 scrollViewDidScroll。然而,我不会用血来签署这个协议。准备好发现循环并采取措施避免它。 (例如在调用setContentOffset之前设置的布尔实例变量,并在scrollViewDidScroll中重新设置,然后return;

React in

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

and set the other's scrollView setContentVisible according to scrollView.contentOffset.

Just be aware that some methods of UIScrollView will invoke scrollViewDidScroll even if called programmatically. This applies to scrollRangeToVisible and will end up in a loop unless you take action to prevent this loop. I don't think that setContentOffset or setting scrollView2.contentOffset = CGPointMake(..,..) does call 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;)

扎心 2024-12-09 06:46:11

继续之前的答案,为了提供更多信息,我生成了以下代码:

在接口 (.h) 中:

#import <UIKit/UIKit.h>

@interface DoubleTextViewController : UIViewController <UITextViewDelegate>

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

@end

在您的实现 (.m) 中:

在定义相应的属性和之后使用此 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];
}

并添加此功能以实现同时滚动。

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

正如 Hermann Klecker 所建议的,isScrolling 变量会停止滚动循环并使用户体验更好。使用 Fabian Kreiser 提出的代码,一旦用户离开手指,滚动就会停止,这很奇怪。

Just continuing with previous answers, to give some more information, I generated this code:

In the interface (.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):

Use this viewDidLoad function after defining the corresponding properties and global variables.

#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. Using the code proposed by Fabian Kreiser makes the scroll stops as soon as the user leaves the finger, making it strange.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文