Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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
Objective c 随机制造?_Objective C_Xcode_Cgpoint - Fatal编程技术网

Objective c 随机制造?

Objective c 随机制造?,objective-c,xcode,cgpoint,Objective C,Xcode,Cgpoint,如何生成随机的CGPointMake,比如说我创建了一个uiimage的出口,并将其命名为image,image.center=CGPointMake(//random,//random)everytime second(NSTimer)您可以在stdlib.h中使用rand() 比如: #include <stdlib.h> #include <time.h> srand ( time(NULL) ); int max = 400 // or what

如何生成随机的CGPointMake,比如说我创建了一个uiimage的出口,并将其命名为image,image.center=CGPointMake(//random,//random)everytime second(NSTimer)

您可以在stdlib.h中使用rand()

比如:

#include <stdlib.h>
#include <time.h>

srand ( time(NULL) );
int max = 400         // or whatever
int myRandomNumber1 = rand() % max
int myRandomNumber2 = rand() % max

image.center = CGPointMake(myRandomNumber1, myRandomNumber2);
#包括
#包括
srand(时间(空));
int max=400//或任何值
int myRandomNumber1=rand()%max
int myRandomNumber2=rand()%max
image.center=CGPointMake(myRandomNumber1,myRandomNumber2);
我会在stdlib.h中使用
arc4random()。这使用了比
rand()
更优越的算法。在手册页中查找此函数。然后,您需要根据两个轴所需的最大坐标修改生成的值

int x = arc4random() % (int) self.view.frame.size.width;
int y = arc4random() % (int) self.view.frame.size.height;

image.center = CGPointMake(x, y);
使用
arc4random()
,您甚至不需要对其进行种子设定

image.center = CGPointMake(arc4random() % yourView.frame.size.width, arc4random() % yourView.frame.size.height);

stdlib.h是C标准库如果我使用此代码,图像会移动很远,然后又非常接近它以前所在的位置,出于某种原因,我会收到一个错误消息,说不允许在QTWO中使用帧。此代码在控制器中吗?如果是这样,请将视图放在self和frame(self.view.frame)之间。我有另一个错误,表示二进制表达式(float和CGFloat)的操作数无效。如何将宽度/高度强制转换为int。强制转换是什么意思?如果我执行视图,那么它会说明如何将float指定给CGFloat*我不知道