在 NSArray 中使用 CGPoint
我一直在尝试创建一个数组,说明我正在开发的应用程序中 UIImageView 的位置。我想要做的是通过使用一个数组,我可以使用它的 x、y 和 z 坐标来存储我的“玩家”图像的位置。我试图完成的脚本看起来像
NSArray *location[3];
-(IBAction)startup;{
[location addObject: player.center.x];
[location addObject: player.center.y];
[location addObject: playerheight];
}
所以我将能够访问这个数组以在屏幕上以“3维”移动我的“玩家”,但我不知道如何将 CGpoint 值转换为 NSValues 所以它们可以在数组中使用,有没有一种简单的方法可以在数组内部执行此操作?
I have been trying to create an array stating the location of a UIImageView in an app I've been working on. What I am trying to do is by using an array I can store the location of my "player" image by using its x,y and z coordinates. The script I am trying to accomplish would look like
NSArray *location[3];
-(IBAction)startup;{
[location addObject: player.center.x];
[location addObject: player.center.y];
[location addObject: playerheight];
}
So I will be able to access this array to move my "player" on the screen in "3-dimensions", but I don't know how to convert the CGpoint values to NSValues so they can be used in the array, is there a simple way to do this inside of the array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要将浮点值转换为对象,请使用 NSNumber。 NSValue 有像 CGPoint 这样的几何类型的包装器。两者都适合你。
To convert floating point values to objects, use NSNumber. NSValue has wrappers for geometric types like CGPoint. Either would work for you.
添加第一个答案。
当您需要从数组中读取 CGPoint 时,您可以使用类似的方法:
To addition for the first answer.
When you'll need to read
CGPoint
back from your array, you can use something like that:另请注意,
NSArray
没有addObject
方法(创建 NSArray 后无法向其添加对象);你想要NSMutableArray
。而不是:
你可能想要更像:
Also note that there's no
addObject
method forNSArray
(you can't add objects to an NSArray after its been created); you wantNSMutableArray
.Instead of:
you probably want something more like:
它必须是 NSArray 吗?为什么不使用结构数组?
或者根据您的设计,声明一个包含 x、y、z 坐标作为 ivars 并将这些对象存储到 NSArray 中的 Objective-C 类可能更有意义。
Does it have to be an NSArray? Why not use an array of structs?
Or depending on your design it may make more sense to declare an objective-C class that contains the x,y,z coordinates as ivars and store those objects into an NSArray.