-4

我想制作一个图像生成器,但我唯一能在任何地方找到的是一个随机的,但我需要它们是一个特定的顺序。

这是我所拥有的,但我不希望图像按随机顺序排列:

.h 文件

@interface ViewController :UIViewController {
    IBOutlet UIImageView *imageview;
    IBOutlet UIImageView *imageview2;
}

-(IBAction)randomimagebutton;

-(IBAction)randomimagebutton2;

@end

.m 文件

-(IBAction)randomimagebutton {

int randomimages = rand() % 8;

switch (randomimages) {
    case 0:            
        imageview.image = [UIImage imageNamed:@"eye right wihte.png"];
        break;

    case 1:
        imageview.image = [UIImage imageNamed:@"eye right red.png"];
        break;

    case 2:
        imageview.image = [UIImage imageNamed:@"eye right pink.png"];
        break;

    case 3:
        imageview.image = [UIImage imageNamed:@"eye right light blue.png"];
        break;

    case 4:
        imageview.image = [UIImage imageNamed:@"eye right dark blue.png"];
        break;

    case 5:
        imageview.image = [UIImage imageNamed:@"eye right light green.png"];
        break;

    case 6:
        imageview.image = [UIImage imageNamed:@"eye right dark green.png"];
        break;

    case 7:
        imageview.image = [UIImage imageNamed:@"eye right orange.png"];
        break;

    case 8:
        imageview.image = [UIImage imageNamed:@"eye right yellow.png"];
        break;

    default:
        break;

}

}

-(IBAction)randomimagebutton2 {

int randomimages = rand() % 8;

switch (randomimages) {
    case 0:
        imageview2.image = [UIImage imageNamed:@"eye left white.png"];
        break;

    case 1:
        imageview2.image = [UIImage imageNamed:@"eye left red.png"];
        break;

    case 2:
        imageview2.image = [UIImage imageNamed:@"eye left pink.png"];
        break;

    case 3:
        imageview2.image = [UIImage imageNamed:@"eye left light blue.png"];
        break;

    case 4:
        imageview2.image = [UIImage imageNamed:@"eye left dark blue.png"];
        break;

    case 5:
        imageview2.image = [UIImage imageNamed:@"eye left light green.png"];
        break;

    case 6:
        imageview2.image = [UIImage imageNamed:@"eye left dark green.png"];
        break;

    case 7:
        imageview2.image = [UIImage imageNamed:@"eye left orange.png"];
        break;

    case 8:
        imageview2.image = [UIImage imageNamed:@"eye left yellow.png"];
        break;

    default:
        break;

}

}
4

1 回答 1

2

好吧,不要使用 ,而是使用int randomimages每次NSNumber获取图像时递增的 an 。使NSNumber类的属性。

更新:

像这样添加属性:

@interface ViewController :UIViewController {
    IBOutlet UIImageView *imageview;
    IBOutlet UIImageView *imageview2;
}

-(IBAction)randomimagebutton;
-(IBAction)randomimagebutton2;

@property (nonatomic, retain) NSNumber *index;

@end

不要忘记@synthesize你.m。

在 .m 文件中,执行以下操作:

- (IBAction)randomimagebutton {

    index = [NSNumber numberWithInt:(([index intValue] + 1) % 8)];

    switch ([index intValue]) {

        ...
于 2012-08-28T13:44:41.063 回答