iphone - 如何使用网页 View 作为页面( subview )制作无缝 ScrollView

标签 iphone objective-c cocoa-touch uiwebview uiscrollview

我想做一个无缝的scrollView ScrollView 应该有 WebView 作为它的 subview 并且应该无缝滚动

问题是... 我引用了

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

现在我已经实现了只有 3 个页面的无缝 ScrollView ,但我希望 webview 代替标签...

最佳答案

尼克 在 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];
}

关于iphone - 如何使用网页 View 作为页面( subview )制作无缝 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9128820/

相关文章:

ios - 在通用应用程序中,如何将模态视图转换或重用为弹出窗口?

ios - 使用 UILabel 调整 uiview 大小并正确设置动画

javascript - 网站(HTML5、JavaScript)能否访问移动设备(android/iPhone)的联系人列表、SDCard 文件

iphone - 保留周期: Why is that such a bad thing?

iphone - 清理 iPhone 模拟器

iOS - UITableView 的 UIRefreshControl 与 UITableViewController 的 UIRefreshControl

iphone - 我应该什么时候 promise ?

ios - PopoverPresentationController不会在触摸外部时被解雇

objective-c - 我应该释放这个 NSString 吗?

ios - UINavigationController 'corrupted navigation bar' 问题