2012-07-31 68 views
0

我只是想知道如果有人能爲我解釋這段代碼,所以我可以從中學習。我試圖讓我的應用程序有一個滾動從左到右滾動的圖片負載(從互聯網),但事情是,它必須有延遲加載。所以我做了一些教程,並想出如何做到這一點,但我真的不明白它。所以我希望某種靈魂可以解釋一步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]; 
    } 
} 

}

+0

您可以節省很多痛苦,並查看Apple示例代碼,該代碼將向您展示如何實現「無限滾動」滾動條,該滾動條根據需要加載圖像。我認爲這是網址,但文件名是InfiniteScrollView.m - http://developer.apple.com/library/ios/#samplecode/StreetScroller/Listings/StreetScroller_InfiniteScrollView_m.html – 2012-07-31 23:07:01

回答

1

關於在scrollView上的延遲加載,我會建議使用UITableView來代替。蘋果公司在這個組件上的表現非常出色。

如果你想連續滾動(pagingEnabled = NO;),你可以讓它們保持水平(見EasyTableView code,效果很好),並停止頁面模式,這樣你就可以得到你正在尋找的行爲。

+0

正在尋找uiscrollview和note uitableview – RollRoll 2013-01-12 16:50:38

0

延遲加載基本上是取大塊的數據(在這個例子中可以說是圖片),只有當你需要它們時。在你的代碼中,你有一個委託方法,當你滾動一個UIScrollView時被調用。 - (void)scrollViewDidScroll:(UIScrollView *)myScrollView函數決定何時實際獲取數據。所以當你滾動時,你會發現你在滾動視圖中的位置(例如你有10個圖像要加載 - 你想知道屏幕當前是否顯示圖像編號1,2,3)等。這就是當前頁面整數的含義。

既然您知道您正在查看哪個頁面,我們希望實際獲取圖像。

if ([myScrollView viewWithTag:(currentPage +1)]) { 
    return; 
} 

上面的代碼檢查當前人看到的圖像是否存在(因此當前頁面+ 1)。如果確實如此,我們已經提取了它,並且我們退出了該功能。否則:

else { 
    // view is missing, create it and set its tag to currentPage+1 
} 

在這裏,我們懶加載圖像。例如,通過創建一個新線程並從服務器下載圖像完成此操作。我們這樣做,而視圖不是當前頁面,因爲我們不希望圖像在用戶滾動時「彈出」。我們添加圖像的視圖獲取標籤(UIView具有「標籤」屬性);我們將標籤設置爲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中移除並釋放一些內存。

希望有幫助。

0

也許這會幫助你。

  1. 從這裏https://github.com/nicklockwood/AsyncImageView/下載總數異步ImageView的文件,並將其包含到您的項目。

  2. 將在廈門國際銀行文件的ImageView並改變它的類AsynchronousImageView而不是UIImageView的

  3. 寫在你的.h文件

    IBOutlet中AsynchronousImageView * _artworkImg;

  4. 寫在你的。m文件

    [_artworkImg loadImageFromURLString:@「Your Image Url」];