0

我有一个包含 4 个不同对象的 NSMutableArray: 还有 4 个 UIImageView:为了从这个数组中获取不同的图像,我编写了一个代码,但它不起作用。请问有什么建议吗?

images = [[NSMutableArray alloc] init];
    UIImage *image1 = [UIImage imageNamed:@"ca.png"];
    [images addObject:image1];
    UIImage *image2 = [UIImage imageNamed:@"ta.png"];
    [images addObject:image2];
    UIImage *image3 = [UIImage imageNamed:@"cv.png"];
    [images addObject:image3];
    UIImage *image4 = [UIImage imageNamed:@"tv.png"];
    [images addObject:image4];

    if (images.count>0) {
        int randomValue = arc4random_uniform(images.count); 
        img1.image = [images objectAtIndex:randomValue];
        img1.tag = randomValue;

        int randomValue1 = arc4random_uniform(images.count);

        if(randomValue1 == img1.tag)
            randomValue1 ++;

        if(randomValue1 > [images count] - 1)
            randomValue1 = 0;

        img2.image = [images objectAtIndex:randomValue1];
        img2.tag = randomValue1;

        int randomValue2 = arc4random_uniform(images.count);

        if (randomValue2 == img1.tag) 
            randomValue2 ++;

        if (randomValue2 == img2.tag)
            randomValue2 ++;

        if (randomValue2 > [images count] - 1)
            randomValue2 = 0;

        img3.image = [images objectAtIndex:randomValue2];
        img3.tag = randomValue2;

        int randomValue3 = arc4random_uniform(images.count);

        if (randomValue3 == img1.tag)
            randomValue3 ++;

        if (randomValue3 == img2.tag)
            randomValue3 ++;

        if (randomValue3 == img3.tag)
            randomValue3 ++;

        if (randomValue3 > [images count] - 1)
            randomValue3 = 0;

        img4.image = [images objectAtIndex:randomValue3];
        img4.tag = randomValue3;

有时它会在 2 或 3 个 UIImageView 中给我相同的图像

4

3 回答 3

0

当您设置第一张图片时

        img1.image = [images objectAtIndex:randomValue];

在这之后你需要

        [images removeObjectAtIndex:randomValue];

但是你只是减少图像数组的数量。

 [images count] - 1

这是不对的。

请注意,如果您不想从原始数组中删除对象。您应该制作图像数组的可变副本。

于 2012-08-31T06:47:39.593 回答
0

首先你随机化你的可变数组。请参阅此问题以了解如何操作。然后只需简单地分配您的图像:

[images shuffle];

img1.image = [images objectAtIndex:0];
img2.image = [images objectAtIndex:1];
img3.image = [images objectAtIndex:2];
img4.image = [images objectAtIndex:3];
于 2012-08-30T12:05:53.533 回答
0

现在我用另一种方式做到了

- (IBAction)ready:(id)sender {

    [ready setHidden:YES];
    [impimage setHidden:NO];
    [img1 setUserInteractionEnabled:YES];
    [img2 setUserInteractionEnabled:YES];
    [img3 setUserInteractionEnabled:YES];
    [img4 setUserInteractionEnabled:YES];


    if (images.count > 0) {
        int randomValue = arc4random_uniform(images.count); 
        impimage.image = [images objectAtIndex:randomValue];

        [img1 setImage:[UIImage imageNamed:@""]];
        [img2 setImage:[UIImage imageNamed:@""]];
        [img3 setImage:[UIImage imageNamed:@""]];
        [img4 setImage:[UIImage imageNamed:@""]];        
    }

}

- (void) onImageClick:(UITapGestureRecognizer*)sender
{   

    UIImageView *m_current = (UIImageView*)sender.view;


    if( [images1 objectAtIndex:m_current.tag] == impimage.image)
    {
        [m_current setImage:[images1 objectAtIndex:m_current.tag]];

        int randomValue = arc4random_uniform(images1.count); 

        if([images1 count])
        {
            if(randomValue == m_current.tag)
                randomValue++;

            if(randomValue > [images1 count] - 1)
                randomValue = 0;

            [impimage setImage:[images1 objectAtIndex:randomValue]];

        }

        NSLog(@"right");
    }

    else NSLog(@"wrong");

    if(img2.image !=nil && img1.image!=nil && img3.image !=nil && img4.image !=nil)
    {
        UIAlertView *alert  = [[UIAlertView alloc] initWithTitle:nil 
                                                         message:@"level completed" 
                                                        delegate:nil cancelButtonTitle:@"OK" 
                                               otherButtonTitles:nil];
        [alert show];

        _level5 = [[Level5 alloc] init];
        [self.view addSubview:_level5.view];
    }

}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [impimage setHidden:YES];
    [img1 setUserInteractionEnabled:NO];
    [img2 setUserInteractionEnabled:NO];
    [img3 setUserInteractionEnabled:NO];
    [img4 setUserInteractionEnabled:NO];

    images = [[NSMutableArray alloc] init];
    UIImage *image1 = [UIImage imageNamed:@"ca.png"];
    [images addObject:image1];
    UIImage *image2 = [UIImage imageNamed:@"ta.png"];
    [images addObject:image2];
    UIImage *image3 = [UIImage imageNamed:@"cv.png"];
    [images addObject:image3];
    UIImage *image4 = [UIImage imageNamed:@"tv.png"];
    [images addObject:image4];

    if (images.count>0) {
        int randomValue = arc4random_uniform(images.count); 
        img1.image = [images objectAtIndex:randomValue];
        img1.tag = randomValue;

        int randomValue1 = arc4random_uniform(images.count);

        if(randomValue1 == img1.tag)
            randomValue1 ++;

        if(randomValue1 > [images count] - 3)
            randomValue1 = 0;

        [images removeObjectAtIndex:img1.tag];

        img2.image = [images objectAtIndex:randomValue1];
        img2.tag = randomValue1;

        int randomValue2 = arc4random_uniform(images.count);

        if (randomValue2 == img2.tag) 
            randomValue2 ++;

        if (randomValue2 > [images count] - 2)
            randomValue2 = 0;

        [images removeObjectAtIndex:img2.tag];

        img3.image = [images objectAtIndex:randomValue2];
        img3.tag = randomValue2;

        int randomValue3 = arc4random_uniform(images.count);

        if (randomValue3 == img3.tag)
            randomValue3 ++;

        if (randomValue3 > [images count] - 1)
            randomValue3 = 0;

        img4.image = [images objectAtIndex:randomValue3];
        img4.tag = randomValue3;

        images1 = [[NSMutableArray alloc] init];
        UIImage *image1 = img1.image;
        [images1 addObject:image1];
        UIImage *image2 = img2.image;
        [images1 addObject:image2];
        UIImage *image3 = img3.image;
        [images1 addObject:image3];
        UIImage *image4 = img4.image;
        [images1 addObject:image4];


    }

    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageClick:)];
    UITapGestureRecognizer* tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageClick:)];
    UITapGestureRecognizer* tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageClick:)];
    UITapGestureRecognizer* tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageClick:)];
    [img1 addGestureRecognizer:tap];
    [img2 addGestureRecognizer:tap1];
    [img3 addGestureRecognizer:tap2];
    [img4 addGestureRecognizer:tap3];
}
于 2012-08-31T04:47:57.723 回答