Iphone &引用;无限UIScrollView“;包含uiwebview
背景:Iphone &引用;无限UIScrollView“;包含uiwebview,iphone,objective-c,uiwebview,uiscrollview,infinite-scroll,Iphone,Objective C,Uiwebview,Uiscrollview,Infinite Scroll,背景: // Sense when the UIScrollView stops accelerating - (void)scrollViewDidEndDecelerating:(UIScrollView *)sender { if(pScrollView.contentOffset.x > pScrollView.frame.size.width) { [self switchToNextPost]; } els
// Sense when the UIScrollView stops accelerating
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
if(pScrollView.contentOffset.x > pScrollView.frame.size.width) {
[self switchToNextPost];
} else if (pScrollView.contentOffset.x < pScrollView.frame.size.width) {
[self switchToPreviousPost];
}
}
// Update the currentPost variable and switch page 1
- (void)switchToNextPost {
[prefs setInteger:[[self getNextKey:nil] intValue] forKey:@"currentPost"];
[self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
}
// Update the currentPost variable and switch page 1
- (void)switchToPreviousPost {
[prefs setInteger:[[self getPreviousKey:nil] intValue] forKey:@"currentPost"];
[self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
}
// Custom delegate function. Scrolls UIScrollView to page 1 and then loads new content on page 1 and 3
- (void)webViewFinishedLoading {
[pScrollView scrollRectToVisible:CGRectMake(pScrollView.frame.size.width, 0, pScrollView.frame.size.width, pScrollView.frame.size.height) animated:NO];
[self loadPostWithKey:[self getPreviousKey:nil] atPage:[NSNumber numberWithInt:0] withViewController:previousPage];
[self loadPostWithKey:[self getNextKey:nil] atPage:[NSNumber numberWithInt:2] withViewController:nextPage];
}
首先是背景。我使用宽度为三页(0、1和2)的UIScrollView创建了一个“无限滚动视图”,然后确保在不滚动时UIScrollView始终位于第1页的中心。当您滚动到第0页或第2页,并且UIScrollViewDelegate感觉到滚动结束时,所有三个页面的内容首先按照您滚动的方向切换,然后UIScrollView立即移回第1页,模拟您可以继续滚动无休止的页面的效果
起初,我使用UITextView来显示每个页面的文本内容,但很快我就希望在设置文本样式时有更灵活的方式。因此,我最终使用了UIWebView,其中加载了一个NSString,其中包含我希望显示的样式化HTML内容。这非常好用,正是我想要的样子
问题:
// Sense when the UIScrollView stops accelerating
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
if(pScrollView.contentOffset.x > pScrollView.frame.size.width) {
[self switchToNextPost];
} else if (pScrollView.contentOffset.x < pScrollView.frame.size.width) {
[self switchToPreviousPost];
}
}
// Update the currentPost variable and switch page 1
- (void)switchToNextPost {
[prefs setInteger:[[self getNextKey:nil] intValue] forKey:@"currentPost"];
[self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
}
// Update the currentPost variable and switch page 1
- (void)switchToPreviousPost {
[prefs setInteger:[[self getPreviousKey:nil] intValue] forKey:@"currentPost"];
[self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
}
// Custom delegate function. Scrolls UIScrollView to page 1 and then loads new content on page 1 and 3
- (void)webViewFinishedLoading {
[pScrollView scrollRectToVisible:CGRectMake(pScrollView.frame.size.width, 0, pScrollView.frame.size.width, pScrollView.frame.size.height) animated:NO];
[self loadPostWithKey:[self getPreviousKey:nil] atPage:[NSNumber numberWithInt:0] withViewController:previousPage];
[self loadPostWithKey:[self getNextKey:nil] atPage:[NSNumber numberWithInt:2] withViewController:nextPage];
}
当然,在UIWebView中加载、解析和显示简单HTML页面所需的时间比在UITextView中加载静态文本所需的时间要长。因此,当您向任何方向滚动,滚动视图自动向后移动并切换页面内容时,您可以在显示新内容之前提示旧内容十分之一秒
我的解决方案(无效):
// Sense when the UIScrollView stops accelerating
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
if(pScrollView.contentOffset.x > pScrollView.frame.size.width) {
[self switchToNextPost];
} else if (pScrollView.contentOffset.x < pScrollView.frame.size.width) {
[self switchToPreviousPost];
}
}
// Update the currentPost variable and switch page 1
- (void)switchToNextPost {
[prefs setInteger:[[self getNextKey:nil] intValue] forKey:@"currentPost"];
[self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
}
// Update the currentPost variable and switch page 1
- (void)switchToPreviousPost {
[prefs setInteger:[[self getPreviousKey:nil] intValue] forKey:@"currentPost"];
[self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
}
// Custom delegate function. Scrolls UIScrollView to page 1 and then loads new content on page 1 and 3
- (void)webViewFinishedLoading {
[pScrollView scrollRectToVisible:CGRectMake(pScrollView.frame.size.width, 0, pScrollView.frame.size.width, pScrollView.frame.size.height) animated:NO];
[self loadPostWithKey:[self getPreviousKey:nil] atPage:[NSNumber numberWithInt:0] withViewController:previousPage];
[self loadPostWithKey:[self getNextKey:nil] atPage:[NSNumber numberWithInt:2] withViewController:nextPage];
}
我在文档中搜索UIWebViewDelegate,发现页面加载完成时发生了一个事件。我把这个连接起来做这个:
包含UIScrollView的视图控制器:
// Sense when the UIScrollView stops accelerating
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
if(pScrollView.contentOffset.x > pScrollView.frame.size.width) {
[self switchToNextPost];
} else if (pScrollView.contentOffset.x < pScrollView.frame.size.width) {
[self switchToPreviousPost];
}
}
// Update the currentPost variable and switch page 1
- (void)switchToNextPost {
[prefs setInteger:[[self getNextKey:nil] intValue] forKey:@"currentPost"];
[self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
}
// Update the currentPost variable and switch page 1
- (void)switchToPreviousPost {
[prefs setInteger:[[self getPreviousKey:nil] intValue] forKey:@"currentPost"];
[self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
}
// Custom delegate function. Scrolls UIScrollView to page 1 and then loads new content on page 1 and 3
- (void)webViewFinishedLoading {
[pScrollView scrollRectToVisible:CGRectMake(pScrollView.frame.size.width, 0, pScrollView.frame.size.width, pScrollView.frame.size.height) animated:NO];
[self loadPostWithKey:[self getPreviousKey:nil] atPage:[NSNumber numberWithInt:0] withViewController:previousPage];
[self loadPostWithKey:[self getNextKey:nil] atPage:[NSNumber numberWithInt:2] withViewController:nextPage];
}
我想你能做的是
因此,您可以将所有web视图设置为空白(不加载任何内容),并在快速滚动时显示活动指示器。谢谢您的回答!事实上,一开始我很想使用这种方法,但后来我改变了主意。我最终没有使用它的原因是,我无法想出一个好方法来跟踪不同视图的位置。现在,我可以很容易地使用变量名previousPost、currentPost和nextPost来知道在哪个子视图中加载哪个帖子,但是当我开始移动它们时,我需要另一种方法来跟踪它。您将如何做到这一点?找到一个解决方案,使用您的技术跟踪子视图。现在它完美无缺了。再次感谢您的回答!你好,Alexander和lukya,我也在实现同样的事情。但无法解决这个问题。所以你们中的任何人都可以帮助我。我在等你的答复。我急需它。请帮助我尽快通过任何教程或任何工作代码。提前谢谢。你好,亚历山大和卢卡亚,我也在实现同样的事情。但无法解决这个问题。所以你们中的任何人都可以帮助我。我在等你的答复。我急需它。请帮助我尽快通过任何教程或任何工作代码。提前谢谢。