2012-11-11 139 views
1

我花了數週的時間搜索並最終在Github上找到了適合我需求的示例。這是一個垃圾拖拉的例子。我現在編輯了我的需求的例子。將圖像陣列添加到UIScrollView ObjectiveC

我現在只有一個問題。在我的滾動視圖中,我有5個圖像,但它們都是一樣的。

我想知道如何使每個縮略圖不同的圖像,所以當我在它改變這種形象我拖着形式滾動視圖頂部的滾動型圖像拖動到ImageView的。

基本上我想在我的scrollview不同的圖像不像我一樣。

我快到了,我被困在了最後一件事上,我需要完成我的戰鬥。

一如往常的任何援助將不勝感激,下面是一些樣本圖像和我的樣本項目的鏈接。

謝謝。

MySampleProject

enter image description here enter image description here enter image description here

回答

3

這是我做過什麼:

一個。重寫GalleryButton類的- initWithFrame:方法是這樣的:

- (id)initWithFrame:(CGRect)frame imageName:(NSString *)imgName 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     isInScrollview = YES; 

     CGRect myImageRect = CGRectMake(0, 0, 64, 64); 
     images = [[UIImageView alloc] initWithFrame:myImageRect]; 

     // here's the change: 
     // instead of a constant name, use the `imgName` parameter 
     [images setImage:[UIImage imageNamed:imgName]]; 
     [self addSubview:images]; 

     self.backgroundColor = [UIColor blackColor]; 
     self.layer.borderWidth = 2; 
     self.layer.borderColor = [UIColor blackColor].CGColor; 
     self.layer.masksToBounds = YES; 
     self.layer.cornerRadius = 5; 
    } 
    return self; 
} 

然後,改寫GalleryScrollView類的這樣的- addAttachment:方法:

- (void) addAttachment:(AttachmentItem *)attachment withImageNamed:(NSString *)imgName 
{ 
    // everything stays the same (!), except this line: 
    GalleryButton *btnAttachment = [[GalleryButton alloc] initWithFrame:CGRectMake(startX, startY, width, height)]; 
    // is to be extended to: 
    GalleryButton *btnAttachment = [[GalleryButton alloc] initWithFrame:CGRectMake(startX, startY, width, height) imageName:imgName]; 
    ... 
} 

然後,在- [HegakaDragAndDropRecycleBinViewController viewDidLoad],指定圖像的文件名,你要使用:

[self.gallery addAttachment:item withImageNamed:@"recyclebin"]; 
[self.gallery addAttachment:item withImageNamed:@"canadian-maple"]; 
[self.gallery addAttachment:item withImageNamed:@"light-cherry"]; 
[self.gallery addAttachment:item withImageNamed:@"mozambique-wenge"]; 
[self.gallery addAttachment:item withImageNamed:@"canadian-maple"]; 

結果:

enter image description here

+0

我爲什麼會得到一個錯誤在這條線:GalleryButton * btnAttachment = [[GalleryButton的alloc] INIT:CGRectMake(STARTX,startY,寬度,高度)imageName:imgName];在我的GalleryScrollView.m「實例方法-init:imageNamed找不到? –

+0

我將如何聲明這我我的頭文件? –

+0

它現在的作品,但我在HegakaDragAndDropRecycleBinViewController viewdidload添加附件withImageNamed未找到與我的圖像[ self.gallery addAttachment:item withImageNamed:@「recyclebin」]; –

0

注:在未來,這將有助於爲你提供你認爲對這個問題明確相關的代碼片段。發佈整個項目和你遇到的問題會有點懶惰。你有什麼嘗試?你在問這裏之前試過什麼,什麼都行不通?

在你GalleryButton.m類中,initWithFrame:方法的代碼:

CGRect myImageRect = CGRectMake(0, 0, 64, 64); 
images = [[UIImageView alloc] initWithFrame:myImageRect]; 
[images setImage:[UIImage imageNamed:@"light-cherry.png"]]; 
[self addSubview:images]; 

變量名images是混亂,因爲它實際上是一個的UIImageView,但這是位置,其中按鈕的圖像正在分配。

解決方案將可以:

  1. 改變initWithFrame:初始化器也接受可以傳遞到UIImageView
  2. 一個UIImage對象添加一個新的方法,以你的GalleryButton稱爲setImage:(UIImage*)image或相似的那臺image在您的UIImageView對象上。

說實話,如果是我的話,我會做兩個,除非你不想要的按鈕被創建後的圖像進行修改,這樣做,在這種情況下,你應該只執行選項1