[英]iOS - Scrolling two UITextViews simultaneously
我已經環顧四周,但找不到任何能夠清楚解釋如何同時滾動兩個不可編輯的UITextView的內容。 我想我可能需要使用scrollRangeToVisible
或setContentOffset
,盡管我無法使它們都起作用。
是否有人可以向我指出任何示例/樣本或相關文檔?
編輯:為澄清起見,我希望能夠滾動一個UITextView,並且將由於滾動而產生的更改也反映在第二個UITextView上。
謝謝!
使用UIScrollViewDelegate方法可獲取有關第一個滾動視圖的滾動動作的信息,然后像下面這樣以編程方式滾動第二個:
- (void) scrollViewDidScroll:(UIScrollView *)view1 {
scrollView2.contentOffset = view1.contentOffset;
}
只是繼續前面的答案,以提供更多的信息,我生成了以下代碼:
在界面(.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提出的代碼,一旦用戶離開手指,滾動就會停止,這很奇怪。
反應在
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
並根據scrollView.contentOffset
設置另一個的scrollView setContentVisible
。
請注意,即使以編程方式調用, UIScrollView
某些方法也會調用scrollViewDidScroll
。 這適用於scrollRangeToVisible,除非您采取措施防止該循環,否則它將最終循環。 我不認為setContentOffset
或設置scrollView2.contentOffset = CGPointMake(..,..)
不會調用scrollViewDidScroll
。 但是,我不會在血液中簽字。 准備看到一個循環並采取措施避免該循環。 (例如在調用setContentOffset
之前設置的布爾實例變量,然后在scrollViewDidScroll
重新設置,然后return;
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.