Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a seamless scrollview with web views as their pages (subviews)

I want to make a seamless scrollView The scrollview should have web views as its subview And should scroll Seamlessly

The question is... I had taken the reference from

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

And now I had implemented the seamless scrollview with only 3 pages, but I want the webviews to be in place of label...

like image 284
Bhanu Birani Avatar asked Feb 03 '12 12:02

Bhanu Birani


1 Answers

Nick In the viewDidLoad we have to write the following code.

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

Now in the loadPageWithID:andPage: method write the following code

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

Now in the scrollViewDidEndDecelerating write the following code:

- (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];
}
like image 103
Bhanu Birani Avatar answered Nov 07 '22 00:11

Bhanu Birani