简体   繁体   中英

Move images to random placeholders

I have a simple jigsaw puzzle i'm making. I have a method that is called when the view is loaded and when the device is shaken. This method places 4 images in 4 specific places on screen. Below is the code:

-(void) makePieceHolders {
//create 4 points where the jigsaw pieces (images) will be placed
CGPoint holder1 = CGPointMake(80, 80);
CGPoint holder2 = CGPointMake(200, 80);
CGPoint holder3 = CGPointMake(80, 200);
CGPoint holder4 = CGPointMake(200, 200);

image1.center = holder1;    //set the position of the image center to one of the newly created points
image1.alpha = 0.3;         //set the image opacity back to 0.3
image2.center = holder2;
image2.alpha = 0.3;
image3.center = holder3;
image3.alpha = 0.3;
image4.center = holder4;
image4.alpha = 0.3;
}

What i'd like is to place the images randomly in the four placeholders. I have some more code written below where i get a random number between 1 and 4 and set the tag of each image to each of these random numbers.

int randomNumber;
int placeHolders[4];
int i=0;
bool numberFound;

do{ // until you get 4 unique numbers
    randomNumber=arc4random()%4+1;
    // Does this number exist already?
    numberFound=FALSE;
    for (int j=0; j<i; j++) {
        if (placeHolders[j]==randomNumber)
            numberFound=TRUE;
    }
    if (numberFound==FALSE){
        placeHolders[i]=randomNumber;
        i++;
    }
} while (i<4);

image1.tag = placeHolders[0];
image2.tag = placeHolders[1];
image3.tag = placeHolders[2];
image4.tag = placeHolders[3];


NSLog(@"img1 tag: %i img2 tag: %i img3 tag: %i img4 tag: %i", image1.tag, image2.tag, image3.tag, image4.tag);

How do now refer to this tag information in order to move it to a placeholder?

In pseudocode i was thinking:

where image tag = 1, move that image to holder1
where image tag = 2, move that image to holder2
............

I don't know how to write this though.

If there is a better way i'd appreciate the help. Thanks

You don't need your complicated do..while / tag logic. Just use an array:

NSMutableArray* images = [NSMutableArray arrayWithObjects: image1,image2,image3,image4,nil];

// shuffle the array
NSUInteger count = [images count];
for (NSUInteger i = 0; i < count; i++) {
    // Select a random element between i and end of array to swap with.
    int nElements = count - i;
    int n = (arc4random() % nElements) + i;
    [images exchangeObjectAtIndex:i withObjectAtIndex:n];
}

After that, you have randomly placed your images in a new order. After that assign the positions:

UIImageView* imageView1 = (UIImageView*)[images objectAtIndex: 0];
imageView.center = holder1;
UIImageView* imageView2 = (UIImageView*)[images objectAtIndex: 1];
imageView.center = holder2;
UIImageView* imageView3 = (UIImageView*)[images objectAtIndex: 2];
imageView.center = holder3;
UIImageView* imageView4 = (UIImageView*)[images objectAtIndex: 3];
imageView.center = holder4;

(You could also do this in a loop.. so it would be more general and reusable.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM