在NSArray中使用CGPoints

Jas*_*son 4 cocoa-touch nsarray cgpoint

我一直在尝试创建一个数组,在我正在研究的应用程序中说明UIImageView的位置.我想要做的是使用一个数组,我可以使用它的x,y和z坐标存储我的"播放器"图像的位置.我试图完成的脚本看起来像

NSArray *location[3];
-(IBAction)startup;{
[location addObject: player.center.x];
[location addObject: player.center.y];
[location addObject: playerheight];
}
Run Code Online (Sandbox Code Playgroud)

所以我将能够访问这个数组,以"三维"的方式在屏幕上移动我的"播放器",但我不知道如何将CGpoint值转换为NSValues,以便它们可以在数组中使用,是否存在在数组内部执行此操作的简单方法?

dra*_*ard 11

要将浮点值转换为对象,请使用NSNumber.NSValue具有CGPoint等几何类型的包装器.要么适合你.

[NSValue valueWithCGPoint:player.center];

[NSNumber numberWithFloat:player.center.x];
[NSNumber numberWithFloat:player.center.y];
Run Code Online (Sandbox Code Playgroud)


Ogr*_*amp 7

为第一个答案添加.当你需要CGPoint从数组中读回来时,你可以使用类似的东西:

CGPoint point = [(NSValue *)[pointsArray objectAtIndex:i] CGPointValue];
Run Code Online (Sandbox Code Playgroud)