問(wèn)題描述
我只是想知道是否有人可以為我解釋這段代碼,以便我可以從中學(xué)習(xí).我試圖讓我的應(yīng)用程序有一個(gè)滾動(dòng)器,它可以從左到右滾動(dòng)大量圖片(來(lái)自互聯(lián)網(wǎng)),但問(wèn)題是,它必須具有延遲加載.所以我做了一些教程并想出了如何去做,但我真的不明白.所以我希望有好心人能解釋一下如何一步一步地延遲加載
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
這是我從教程中學(xué)到的代碼:
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];
}
}
}
推薦答案
關(guān)于滾動(dòng)視圖的延遲加載,我強(qiáng)烈建議改用 UITableView.蘋(píng)果在這個(gè)組件的性能方面做得很好.
About Lazy loading on scrollView, I would greatly advised to use UITableView instead. Apple did a great job with performance on this component.
您可以將它們水平放置(請(qǐng)參閱此 EasyTableView 代碼,效果很好)并停止頁(yè)面模式如果您想要連續(xù)滾動(dòng)(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.
這篇關(guān)于iOS UIScrollView 延遲加載的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!