【问题标题】:How to make a seamless scrollview with web views as their pages (subviews)如何使用 Web 视图作为其页面(子视图)制作无缝滚动视图
【发布时间】:2012-02-26 01:56:30
【问题描述】:

我想做一个无缝的scrollView 滚动视图应该有 web 视图作为它的子视图 并且应该无缝滚动

问题是... 我参考了

http://iosdevelopertips.com/user-interface/creating-circular-and-infinite-uiscrollviews.html#comment-66679

现在我已经实现了只有 3 个页面的无缝滚动视图,但我希望 webviews 代替标签...

【问题讨论】:

  • 恭喜,但你的问题在哪里?
  • 问题是...我从iphonedevelopertips.com/user-interface/… 获取了参考,现在我已经实现了只有 3 页的无缝滚动视图,但我希望 webviews 代替标签...
  • 你尝试了什么?出了什么问题?
  • 我曾尝试使用 webviews 代替 viewDidLoad 中的标签。并在 -(void)loadPageWithId: onPage: 方法中加载了每个 webView,但它没有重用它们。
  • 我希望滚动视图中应该只有 3 个 webviews,当我向左滑动时,所有 webviews 都应该移动,并且新的 webviews 会在第三个中加载。

标签: iphone objective-c cocoa-touch uiwebview uiscrollview


【解决方案1】:

尼克 在viewDidLoad中我们要写如下代码。

- (void)viewDidLoad 
{
    [super viewDidLoad];

    documentTitles = [[NSMutableArray alloc] init];
    arrayWebViews = [[NSMutableArray alloc] init];

    // create our array of documents
    for (int i = 0; i < 5; i++) 
    {
        [documentTitles addObject:[NSString stringWithFormat:@"index%i",i]];
    }

    // create webviews for the html files

    webOne = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
    webTwo = [[UIWebView alloc] initWithFrame:CGRectMake(320, 0, 320, 400)];
    webThree = [[UIWebView alloc] initWithFrame:CGRectMake(640, 0, 320, 400)];

    // load all three pages into our scroll view
    [self loadPageWithId:2 onPage:0];
    [self loadPageWithId:0 onPage:1];
    [self loadPageWithId:1 onPage:2];

    // add them as the subview of scrollview
    [scrollView addSubview:webOne];
    [scrollView addSubview:webTwo];
    [scrollView addSubview:webThree];

    // adjust content size for three pages of data and reposition to center page
    scrollView.contentSize = CGSizeMake(960, 400);  
    [scrollView scrollRectToVisible:CGRectMake(320,0,320,400) animated:NO];
}

现在在 loadPageWithID:andPage: 方法中编写如下代码

- (void)loadPageWithId:(int)index onPage:(int)page 
{
    switch (page) 
    {
        case 0:
            {
                self.webOne.backgroundColor = [UIColor clearColor];
                self.webOne.delegate = self;
                [self.webOne loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]                                                                                              pathForResource:[documentTitles objectAtIndex:index] ofType:@"html"]isDirectory:NO]]];
                [arrayWebViews addObject:self.webOne];
            }
            break;
}

现在在scrollViewDidEndDecelerating中写下如下代码:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender 
{    
    UIWebView* lobjTempWebView = [arrayWebViews objectAtIndex:0];

    for (int indexWebViews = 0; indexWebViews < 2; indexWebViews ++)
    {
        [arrayWebViews replaceObjectAtIndex:indexWebViews withObject:[arrayWebViews objectAtIndex:indexWebViews + 1]];
    }

    [arrayWebViews replaceObjectAtIndex:[arrayWebViews count] - 1 withObject:lobjTempWebView];

    [[arrayWebViews objectAtIndex:0] setFrame:CGRectMake(0, 0, 320, 400)];
    [[arrayWebViews objectAtIndex:1] setFrame:CGRectMake(320, 0, 320, 400)];
    [[arrayWebViews objectAtIndex:2] setFrame:CGRectMake(640, 0, 320, 400)];

    if(scrollView.contentOffset.x > scrollView.frame.size.width) 
    {

        currIndex = (currIndex >= [documentTitles count]-1) ? 0 : currIndex + 1;
        nextIndex = (currIndex >= [documentTitles count]-1) ? 0 : currIndex + 1;         
        [[arrayWebViews objectAtIndex:2] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]                                                                                              pathForResource:[documentTitles objectAtIndex:nextIndex] ofType:@"html"]isDirectory:NO]]];

    }     
    // Reset offset back to middle page     
    [scrollView scrollRectToVisible:CGRectMake(320,0,320,400) animated:NO];
}

【讨论】:

    【解决方案2】:

    你永远无法做到无缝。 Webviews 需要一段时间来加载它们的内容。如果你像这样回收它们,它们会在重新加载内容时暂时空白。

    【讨论】:

    • 嗨尼克,我们可以使用任何其他技术制作带有 Web 视图的无缝滚动视图吗?
    • 为什么不在这里发布解决方案作为答案,因为您似乎已经解决了一个其他人都不知道一个好的解决方案的问题?
    • 嗨尼克..我已经附上了上面的答案。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 2020-06-22
    相关资源
    最近更新 更多