2012-02-03 29 views
4

无缝滚动视图我想要的无缝滚动视图 滚动视图应该具有Web视图作为其子视图 并应滚动无缝如何使网站的意见,他们的网页(子视图)

的问题是... 我采取了从

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

参考现在我已经实现无缝滚动型只有3页,但我想在网页视图要到位标签...

+0

祝贺,但如果是你的问题? – 2012-02-03 12:45:12

+0

现在的问题是... 我已经参考了 http://iphonedevelopertips.com/user-interface/creating-circular-and-infinite-uiscrollviews.html#comment-66679 现在我已经实现了无缝只有3页的滚动视图,但我希望网页视图代替标签... – 2012-02-03 12:50:18

+0

你试过了什么?出了什么问题? – wattson12 2012-02-03 13:04:46

回答

5

Nick 在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]; 
} 
0

你永远不会觉得这是无缝的。 Webviews需要一段时间来加载其内容。如果你像这样回收它们,当它们的内容被重新加载时它们将空白一段时间。

+1

嗨,尼克,我们可以使用任何其他技术使用web视图进行无缝滚动视图。 – 2012-02-06 04:58:49

+1

得到了解决方案....感谢所有的帮助....非常感谢.... – 2012-02-06 13:08:56

+0

为什么你不把解决方案作为答案发布在这里,因为你似乎解决了一个问题,没有人知道一个很好的解决方案? – 2012-02-06 18:22:17

相关问题