iphone - 将图像移动到随机占位符

标签 iphone objective-c ios image random

我正在制作一个简单的拼图游戏。我有一个在加载 View 和摇动设备时调用的方法。此方法将 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);

现在如何引用此标签信息以将其移动到占位符?

在伪代码中我在想:

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

虽然我不知道怎么写。

如果有更好的方法,我将不胜感激。谢谢

最佳答案

您不需要复杂的 do..while/标记逻辑。 只需使用数组即可:

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;

(您也可以在循环中执行此操作..这样它会更通用且可重用。)

关于iphone - 将图像移动到随机占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9861956/

相关文章:

iphone - 如何在后台保持 iphone ios xmpp 连接?

iphone - UIWebview 中的 Youtube 视频不起作用

iphone - 如何从另一个 UITabBarController 中的另一个 UINavigationController 转到一个 UITabBarController 中的顶级 UINavigationController?

iphone - OpenGL ES 2D - z 顺序、深度缓冲区与按顺序绘制

objective-c - 使应用程序与以前的 iOS 版本兼容

ios - HTML 的 <br>、<h1>、<p> 标签无法与 UILabel 属性字符串一起正常工作

ios - Swift 覆盖实例变量

iphone - 更改 MKMapView 中的 MKMapType 并保留自定义 pinImage 用于注释

ios - 物理体的轮廓通过我的 swift SpriteKit 游戏中的所有其他节点显示

iphone - 从 UIView 类访问方法