问题描述
我只是想知道是否有人可以为我解释这段代码,以便我可以从中学习.我试图让我的应用程序有一个滚动器,它可以从左到右滚动大量图片(来自互联网),但问题是,它必须具有延迟加载.所以我做了一些教程并想出了如何去做,但我真的不明白.所以我希望有好心人能解释一下如何一步一步地延迟加载
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]; } }
}
推荐答案
关于滚动视图的延迟加载,我强烈建议改用 UITableView.苹果在这个组件的性能方面做得很好.
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.