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

今、私は別の方法でそれを作りました

- (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 に答える
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

最初の画像を設定するとき

        img1.image = [images objectAtIndex:randomValue];

この後、必要です

        [images removeObjectAtIndex:randomValue];

しかし、あなたは画像配列の数を減らすだけです。

 [images count] - 1

間違いです。

元の配列からオブジェクトを削除したくない場合は注意してください。画像配列の変更可能なコピーを作成する必要があります。

于 2012-08-31T06:47:39.593 に答える