iOS UIScrollView延迟加载 [英] iOS UIScrollView Lazy Loading
问题描述
我只是想知道是否有人可以为我解释这个代码,所以我可以从中学习。我试图让我的应用程序有一个滚动从左到右滚动与大量的图片(从互联网)但事实是,它必须有延迟加载。所以我做了一些教程,并想出如何做到但我真的不理解它。所以我希望有些灵魂会逐步解释如何延迟加载
i was just wondering if someone could explain this code for me so i can actually learn from it. I am trying to make my app have a scroller that scrolls left to right with loads of pictures (from internet) but the thing is, it must have lazy loading. so i did some tutorials and figured out how to do it but i truly don't understand it. So i was hoping some kind soul would explain how to lazy load step by step
这是我从教程中学到的代码:
This is the code i had learned from the tutorials:
-(void)scrollViewDidScroll:(UIScrollView *)myScrollView {
/**
* calculate the current page that is shown
* you can also use myScrollview.frame.size.height if your image is the exact size of your scrollview
*/
int currentPage = (myScrollView.contentOffset.y / currentImageSize.height);
// display the image and maybe +/-1 for a smoother scrolling
// but be sure to check if the image already exists, you can do this very easily using tags
if ( [myScrollView viewWithTag:(currentPage +1)] ) {
return;
}
else {
// view is missing, create it and set its tag to currentPage+1
}
/**
* using your paging numbers as tag, you can also clean the UIScrollView
* from no longer needed views to get your memory back
* remove all image views except -1 and +1 of the currently drawn page
*/
for ( int i = 0; i < currentPages; i++ ) {
if ( (i < (currentPage-1) || i > (currentPage+1)) && [myScrollView viewWithTag:(i+1)] ) {
[[myScrollView viewWithTag:(i+1)] removeFromSuperview];
}
}
}
推荐答案
关于在scrollView上加载延迟,我非常建议使用UITableView。 Apple在这个组件上的表现做得很好。
About Lazy loading on scrollView, I would greatly advised to use UITableView instead. Apple did a great job with performance on this component.
您可以将它们保持水平(请参阅此 EasyTableView代码,效果很好)如果你想连续滚动(pagingEnabled = NO;)就停止页面模式,这样你就可以得到你想要的行为。
You can have them horizontal (see this EasyTableView code, it works great) and stop the page mode if you want a continuous scroll (pagingEnabled = NO;) so you'll be able to get the behavior you are looking for.
这篇关于iOS UIScrollView延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!