简体   繁体   中英

In iOS App, how to scroll two UIView simultaneously

I am developing iOS App.

I add UITextView(100px height) on top of UIWebView and would like to make them scroll simultaneously.

I am writing down the following code. However, when I scroll my screen, only UIWebView is scrolled and UITextView isn't.(UITextView is fixed!)

I hope that when I scroll up my screen, both UIWebView and UITextView go up, and vice versa.

Could you tell me how to solve?

@interface DetailViewController ()<UIWebViewDelegate>{
     UITextView *recommendText;
}

@property (weak, nonatomic) IBOutlet UIWebView *detailPage;

- (void)viewWillAppear:(BOOL)animated{
    [self headerMake];
}

-(void)headerMake{

    recommendText = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320,100)];
    recommendText.layer.borderWidth = 2;
    recommendText.text = @"hogehoge";
    [self.detailPage addSubview:recommendText];

    [[self.detailPage scrollView] setContentInset:UIEdgeInsetsMake(100, 0, 0, 0)];

}

A text view is a scroll view subclass, and web views have a scrollView property. Listen to both scroll views content offset change (using scrollViewDidScroll: ) and update the content offset of the other scroll view accordingly. This will give you the effect of scrolling the two at the same time.

self.textview.delegate = self;
self.webview.scrollView.delegate = self;

...

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView == self.textview)
    {
        self.webview.scrollView.contentOffset = self.textview.contentOffset;
    }
    else
    {
        self.textview.contentOffset = self.webview.scrollView.contentOffset;
    }
}

If i understand your question correctly you should embed them both is a scroll view and disable scrolling in the UIWebview and the UITextView. then they will scroll as one. Although you may have to dynamically set the sizes of the Textview and webview if they have different content they have to support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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