Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将图像移动到随机占位符_Iphone_Objective C_Ios_Image_Random - Fatal编程技术网

Iphone 将图像移动到随机占位符

Iphone 将图像移动到随机占位符,iphone,objective-c,ios,image,random,Iphone,Objective C,Ios,Image,Random,我正在做一个简单的拼图游戏。我有一个方法,在加载视图和晃动设备时调用。此方法在屏幕上的4个特定位置放置4个图像。代码如下: -(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

我正在做一个简单的拼图游戏。我有一个方法,在加载视图和晃动设备时调用。此方法在屏幕上的4个特定位置放置4个图像。代码如下:

-(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;
}
我想要的是将图像随机放置在四个占位符中。下面我写了一些代码,其中我得到了一个介于1和4之间的随机数,并将每个图像的标记设置为这些随机数中的每一个

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);
我不知道怎么写这个


如果有更好的办法,我会感谢你的帮助。谢谢

您不需要复杂的do..while/tag逻辑。 只需使用一个数组:

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];
}

(您也可以在一个循环中执行此操作。因此它将更加通用和可重用。)

太好了,谢谢。现在很好用。这种随机化方法是否适用于更大尺寸的拼图?是的,它适用于任何尺寸的阵列。它确保每个元素都至少一次改变了另一个元素的位置。(附言:我们可以删除我们的旧评论。它们不会帮助任何人;)
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];
}
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;