2011-11-15 136 views
0

我從服務器加載預覽圖像(縮略圖)並將它們保存到本地文檔目錄。對於每個圖像都有一個核心數據條目。iOS創建UIButton的矩陣

現在我需要將這個縮略圖渲染成ScrollView。我不知道將來會有多少縮略圖,爲什麼我需要以編程方式將縮略圖渲染到ScrollView中。我知道我必須根據縮略圖的數量來設置scrollView的高度。

縮略圖需要是可觸摸的,導致在縮略圖上點擊應打開另一個對話。

第一個問題:什麼是正確的控制用於顯示縮略圖?是否將UIButton作爲自定義按鈕並將縮略圖設置爲Background-Image?

第二個問題:如何設置一個動態矩陣來渲染拇指(按鈕)。

感謝

回答

1

第一個答案:我會建議使用用於此目的的UIButton,這就是我想在這種情況下做

第二個答案:假設你有某種所有的數組你縮略圖,那麼你可以簡單地遍歷他們確實創造了所有的按鈕在類似這樣的方式:

NSArray *thumbnailImages; 
UIScrollView *scrollView; 
//Scrollview is linked up through IB or created dynamically...whatever is easier for you 
//The thumbnailImages array is populated by your list of thumbnails 

//Assuming that your thumbnails are all the same size: 
const float thumbWidth = 60.f;//Just a random number for example's sake 
const float thumbHeight = 90.f; 
//Determine how many thumbnails per row are in your matrix. 
//I use 320.f as it is the default width of the view, use the width of your view if it is not fullscreen 
const int matrixWidth = 320.f/thumbWidth; 

const int contentHeight = thumbHeight * (thumbnailImages.count/matrixWidth); 

for (int i = 0; i < thumbnailImages.count; ++i) 
{ 

    int columnIndex = i % matrixWidth; 
    int rowIndex = i/matrixWidth;//Intentionally integer math 

    UIImage* thumbImage = [thumbnailImages objectAtIndex:i]; 
    UIButton* thumbButton = [[UIButton alloc] initWithFrame:CGRectMake(columnIndex*thumbWidth, rowIndex*thumbHeight, thumbWidth, thumbHeight)]; 

    thumbButton.imageView.image = thumbImage; 

    [scrollView addSubView:thumbButton]; 

    [thumbButton release]; 
} 

[scrollView setContentSize: CGSizeMake(320.f, contentHeight)]; 

不必採取這種逐字逐句的代碼,我只是在記事本寫了這件事,但它應該給你g如何做到這一點的普遍想法

+0

好的,這個工程。但是我發現了一個要求,那就是不可能使用UIButton。如果用戶長時間輕敲並將手指放在縮略圖上,則用戶應該有可能刪除縮略圖以及文檔目錄中的基礎核心數據和數據。任何想法? – MadMaxAPP

+0

嗯,我不確定那一個。我不知道iOS中如何正常處理這些類型的東西,如果有某種類型的holdgesture識別器。你應該在這裏檢查UIButton類的引用:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html,看看是否有你的事件或選擇器需要。 –

+0

您也可以創建自己的自定義視圖,該視圖使用UIImageView,或者以某種方式使用UIButton子類,但是如何設置矩陣的一般想法是我如何在上面概述它。 –