在 NSArray 中使用 CGPoint

发布于 2024-09-04 00:07:44 字数 402 浏览 7 评论 0原文

我一直在尝试创建一个数组,说明我正在开发的应用程序中 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

单身狗的梦 2024-09-11 00:07:44

要将浮点值转换为对象,请使用 NSNumber。 NSValue 有像 CGPoint 这样的几何类型的包装器。两者都适合你。

[NSValue valueWithCGPoint:player.center];

[NSNumber numberWithFloat:player.center.x];
[NSNumber numberWithFloat:player.center.y];

To convert floating point values to objects, use NSNumber. NSValue has wrappers for geometric types like CGPoint. Either would work for you.

[NSValue valueWithCGPoint:player.center];

[NSNumber numberWithFloat:player.center.x];
[NSNumber numberWithFloat:player.center.y];
奢欲 2024-09-11 00:07:44

添加第一个答案。
当您需要从数组中读取 CGPoint 时,您可以使用类似的方法:

CGPoint point = [(NSValue *)[pointsArray objectAtIndex:i] CGPointValue];

To addition for the first answer.
When you'll need to read CGPoint back from your array, you can use something like that:

CGPoint point = [(NSValue *)[pointsArray objectAtIndex:i] CGPointValue];
如梦初醒的夏天 2024-09-11 00:07:44

另请注意,NSArray 没有 addObject 方法(创建 NSArray 后无法向其添加对象);你想要NSMutableArray

而不是:

NSArray *location[3];

你可能想要更像:

NSMutableArray *location = [NSMutableArray arrayWithCapacity:3];

Also note that there's no addObject method for NSArray (you can't add objects to an NSArray after its been created); you want NSMutableArray.

Instead of:

NSArray *location[3];

you probably want something more like:

NSMutableArray *location = [NSMutableArray arrayWithCapacity:3];
过期以后 2024-09-11 00:07:44

它必须是 NSArray 吗?为什么不使用结构数组?

typedef struct {
    CGPoint location;
    CGFloat height;
} PlayerLocation;

PlayerLocation players[3];

players[0].location = player.center;
players[0].height   = playerheight;

或者根据您的设计,声明一个包含 x、y、z 坐标作为 ivars 并将这些对象存储到 NSArray 中的 Objective-C 类可能更有意义。

@interface PlayerLocation : NSObject {
  CGPoint location;
  CGFloat height;
}
@end

Does it have to be an NSArray? Why not use an array of structs?

typedef struct {
    CGPoint location;
    CGFloat height;
} PlayerLocation;

PlayerLocation players[3];

players[0].location = player.center;
players[0].height   = playerheight;

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.

@interface PlayerLocation : NSObject {
  CGPoint location;
  CGFloat height;
}
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文