Iphone 如何以web视图作为页面(子视图)创建无缝滚动视图
我想创建一个无缝的滚动视图 scrollview应将web视图作为其子视图 并且应该无缝滚动 问题是。。。 我是从他的信中得到这封信的Iphone 如何以web视图作为页面(子视图)创建无缝滚动视图,iphone,objective-c,cocoa-touch,uiwebview,uiscrollview,Iphone,Objective C,Cocoa Touch,Uiwebview,Uiscrollview,我想创建一个无缝的滚动视图 scrollview应将web视图作为其子视图 并且应该无缝滚动 问题是。。。 我是从他的信中得到这封信的 现在我已经实现了只有3个页面的无缝滚动视图,但我希望Web视图能够取代标签…你永远无法实现无缝滚动视图。Web视图需要一段时间来加载其内容。如果你像这样回收它们,当它们的内容被重新加载时,它们会有一段时间是空白的。 在viewDidLoad中,我们必须编写以下代码 - (void)viewDidLoad { [super viewDidLoad];
现在我已经实现了只有3个页面的无缝滚动视图,但我希望Web视图能够取代标签…你永远无法实现无缝滚动视图。Web视图需要一段时间来加载其内容。如果你像这样回收它们,当它们的内容被重新加载时,它们会有一段时间是空白的。 在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];
}
现在在ScrollViewDiEndDecelling中编写以下代码:
- (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];
}
-(无效)ScrollViewDiEndDecelling:(UIScrollView*)发送方
{
UIWebView*lobjTempWebView=[arrayWebViews对象索引:0];
对于(int indexWebViews=0;indexWebViews<2;indexWebViews++)
{
[arrayWebViews replaceObjectAtIndex:indexWebViews with Object:[arrayWebViews objectAtIndex:indexWebViews+1];
}
[arrayWebViews replaceObjectAtIndex:[arrayWebViews计数]-1,对象:lobjTempWebView];
[[arrayWebViews对象索引:0]setFrame:CGRectMake(0,0320400)];
[[arrayWebViews对象索引:1]setFrame:CGRectMake(320,0320400)];
[[arrayWebViews对象索引:2]setFrame:CGRectMake(640,0320400)];
if(scrollView.contentOffset.x>scrollView.frame.size.width)
{
currIndex=(currIndex>=[文档标题计数]-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]类型:@“html”]isDirectory:NO]];
}
//将偏移量重置回中间页
[scrollView scrollRectToVisible:CGRectMake(320,0320400)动画:否];
}
祝贺你,但你的问题在哪里?问题是。。。我从中获得了参考,现在我实现了只有3页的无缝scrollview,但我希望WebView取代标签…您尝试了什么?出了什么问题?我试着用WebView代替viewDidLoad中的标签。并在-(void)loadPageWithId:onPage:method中加载了每个Web视图,但它没有重用它们。我希望scrollview中只有3个Web视图,当我向左滑动时,所有Web视图都应该移动,新的Web视图将在第三个加载。嗨,Nick,我们可以使用任何其他技术使scrollview与web view无缝结合。既然你似乎已经解决了一个其他人都不知道好的解决方案的问题,为什么不在这里发布解决方案作为答案呢?嗨,尼克。。我已经附上了上面的答案。谢谢
- (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];
}