xcode CGPoint 到 NSValue 并反转
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11327249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CGPoint to NSValue and reverse
提问by hockeyman
I have code:
我有代码:
NSMutableArray *vertices = [[NSMutableArray alloc] init];
//Getting mouse coordinates
loc = [self convertPoint: [event locationInWindow] fromView:self];
[vertices addObject:loc]; // Adding coordinates to NSMutableArray
//Converting from NSMutableArray to GLfloat to work with OpenGL
int count = [vertices count] * 2; // * 2 for the two coordinates of a loc object
GLFloat []glVertices = (GLFloat *)malloc(count * sizeof(GLFloat));
int currIndex = 0;
for (YourLocObject *loc in vertices) {
glVertices[currIndex++] = loc.x;
glVertices[currIndex++] = loc.y;
}
loc
is CGPoint, so i need somehow to change from CGPoint to NSValue to add it to NSMutableArray and after that convert it back to CGPoint. How could it be done?
loc
是 CGPoint,所以我需要以某种方式从 CGPoint 更改为 NSValue 以将其添加到 NSMutableArray,然后将其转换回 CGPoint。怎么可能呢?
回答by Vadim
The class NSValue
has methods +[valueWithPoint:]
and -[CGPointValue]
? Is this what you are looking for?
该类NSValue
有方法+[valueWithPoint:]
和-[CGPointValue]
? 这是你想要的?
//Getting mouse coordinates
NSMutableArray *vertices = [[NSMutableArray alloc] init];
CGPoint location = [self convertPoint:event.locationInWindow fromView:self];
NSValue *locationValue = [NSValue valueWithPoint:location];
[vertices addObject:locationValue];
//Converting from NSMutableArray to GLFloat to work with OpenGL
NSUInteger count = vertices.count * 2; // * 2 for the two coordinates
GLFloat GLVertices[] = (GLFloat *)malloc(count * sizeof(GLFloat));
for (NSUInteger i = 0; i < count; i++) {
NSValue *locationValue = [vertices objectAtIndex:i];
CGPoint location = locationValue.CGPointValue;
GLVertices[i] = location.x;
GLVertices[i] = location.y;
}