2010-08-01 94 views
1

我有一個UIImageView *picture更改圖像使用按鈕iPhone

UIButton *next

- (IBAction)next { 
} 

我想改變的觀點,但只有當圖像等於圖像...例如img1

但使用相同的按鈕我想也能夠改變圖片,如果圖像= img2,但不同的圖像(img3)

到目前爲止,我有這樣的代碼,但它給我的錯誤:

- (IBAction)next { 
    if (picture UIImage = imageNamed:@"img01.jpg") { 
    [picture setImage: [UIImage imageNamed:@"img02.jpg" 
    } 
    if (picture UIImage = imageNamed:@"img02.jpg") { 
    [picture setImage: [UIImage imageNamed:@"img03.jpg" 
} 
} 

回答

1

我想通了,現在我所做的就是忘記把.JPG上的IMG%我結束;)

- (IBAction)next { 
    static int index = 0; // <-- here 
    index++; 
    // Set imageCount to as many images as are available 
    int imageCount=16; 
    if (index<=imageCount) { 
     NSString* imageName=[NSString stringWithFormat:@"img%i", index]; 
     [picture setImage: [UIImage imageNamed: imageName]]; 
    } 
} 
+0

除非你有迫切的需求,否則你不應該使用靜態變量。你現在沒有辦法獲得圖像索引。 – mvds 2010-08-01 13:13:03

0
- (IBAction)next { 
    if ([[picture image] isEqual: [UIImage imageNamed:@"img01.jpg"]]) { 

     [picture setImage: [UIImage imageNamed:@"img02.jpg"]]; 

    } else if ([[picture image] isEqual: [UIImage imageNamed:@"img02.jpg"]]) { 

     [picture setImage: [UIImage imageNamed:@"img03.jpg"]]; 
    } 
} 

此外,這是改變圖像的一個非常基本的方式。一個更優雅的方式將是有一個全球int有當前的圖像索引,所以點擊「下一步」將簡單地增加該計數。然後,如果圖像具有該名稱存在,切換到它:

//聲明指數Header.h

index=0; 

- (IBAction)next { 
    index++; 
    // Set imageCount to as many images as are available 
    int imageCount=2; 
    if (index<=imageCount) { 
     NSString* imageName=[NSString stringWithFormat:@"img%02i", index]; 
     [picture setImage: [UIImage imageNamed: imageName]]; 
    } 
} 
+0

它只是退出應用程序? – Will 2010-08-01 11:03:10

+0

請你能幫助它說UIImageView可能不會響應UIImage – Will 2010-08-01 11:06:24

+0

EDITED-對不起,我忘記了結束「)」在if語句。另外,我的第二個例子可能對使用超過3張圖像更有用。 – pop850 2010-08-01 11:11:32

0

子類UIImageView和在頭添加enum屬性:

typedef enum _PictureType { 
    PictureTypeFirstImage = 0, 
    PictureTypeSecondImage, 
    PictureTypeThirdImage, 
    PictureTypes 
} PictureType; 

@interface MyImageView : UIImageView { 
    PictureType type; 
} 

@property (readwrite) PictureType type; 

執行中:

@synthesize type; 

初始化您的picture

[picture setImage:[UIImage imageNamed:@"img01.jpg"]]; 
picture.type = PictureTypeFirstImage; 

在動作方法:

- (IBAction) next { 
    switch (picture.type) { 
     case (PictureTypeFirstImage): { 
      [picture setImage:[UIImage imageNamed:@"img02.jpg"]]; 
      picture.type = PictureTypeSecondImage; 
      break; 
     } 
     case (PictureTypeSecondImage): { 
      [picture setImage:[UIImage imageNamed:@"img03.jpg"]]; 
      picture.type = PictureTypeThirdImage; 
      break; 
     } 
     default: 
      break; 
    } 
} 
+0

錯誤:在執行前可能沒有指定類型或存儲類別 /Users/Will/Desktop/SUC/Classes/Pictures.m:15:0/ Users/Will/Desktop/SUC/Classes/Pictures。 m:15:錯誤:期望的標識符或'('''合成'之前' /用戶/疑問/桌面/SUC/Classes/Pictures.m:41:0 /用戶/將/桌面/ SUC /類/圖片。 m:41:致命錯誤:方法聲明不在@interface上下文中 – Will 2010-08-01 11:12:04

+0

我得到那些錯誤請幫忙 錯誤:沒有類型或存儲類可能在此處指定'執行' 錯誤:預期標識符或'('before'綜合 – Will 2010-08-01 11:13:00

+0

當你繼承UIImageView時,子類需要自己獨立的頭文件和實施文件。 – 2010-08-01 11:14:49

1
- (IBAction)next { 
    picture.tag++; 
    [picture setImage:[UIImage imageNamed: 
      [NSString stringWithFormat:@"img%02d.jpg",1+(picture.tag%2)] 
    ]]; 
} 

應該是最簡單的解決方案。

編輯第一次點擊,去img02.jpg,第二次再次點擊返回到img01.jpg。增加2允許img03.jpg

+0

哇,我真的推翻了我的回答... – pop850 2010-08-01 13:12:32