来自NSMutableArray的不同UIImageView的不同UIImages

时间:2012-08-30 01:45:40

标签: objective-c arrays random uiimageview uiimage

我有一个由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的

中为我提供相同的图像

3 个答案:

答案 0 :(得分:0)

首先,你随机化你的可变数组。请参阅this question了解具体方法。然后只需简单地分配您的图像:

[images shuffle];

img1.image = [images objectAtIndex:0];
img2.image = [images objectAtIndex:1];
img3.image = [images objectAtIndex:2];
img4.image = [images objectAtIndex:3];

答案 1 :(得分: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];
}

答案 2 :(得分:0)

设置第一张图像时

        img1.image = [images objectAtIndex:randomValue];

在此之后你需要

        [images removeObjectAtIndex:randomValue];

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

 [images count] - 1

这不对。

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