我只是想知道如果有人能爲我解釋這段代碼,所以我可以從中學習。我試圖讓我的應用程序有一個滾動從左到右滾動的圖片負載(從互聯網),但事情是,它必須有延遲加載。所以我做了一些教程,並想出如何做到這一點,但我真的不明白它。所以我希望某種靈魂可以解釋一步iOS UIScrollView懶加載
如何偷懶負載階躍這是我從教程瞭解到代碼:
-(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];
}
}
}
您可以節省很多痛苦,並查看Apple示例代碼,該代碼將向您展示如何實現「無限滾動」滾動條,該滾動條根據需要加載圖像。我認爲這是網址,但文件名是InfiniteScrollView.m - http://developer.apple.com/library/ios/#samplecode/StreetScroller/Listings/StreetScroller_InfiniteScrollView_m.html – 2012-07-31 23:07:01