2010-08-01 162 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