![](/img/trans.png)
[英]Cocoa: Problem passing a CGPoint with NSNotification and NSDictionary
[英]NSDictionary to CGPoint
我正在开发一个游戏,在这种游戏中,如果子弹分别单击和双击,我将尝试发射两种不同的类型。
这是我在接触中开始做的事情的方法:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for( UITouch *touch in touches )
{
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
NSLog(@"TOUCH LOCATION IN TOUCH BEGAN = (%f , %f)", location.x , location.y);
NSUInteger tapCount = [touch tapCount];
switch (tapCount)
{
case 1:
{
NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithCGPoint:location] forKey:@"location"];
[self performSelector:@selector(startTimer:) withObject:touchloc afterDelay:3];
break;
}
case 2:
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startTimer) object:nil];
[self performSelector:@selector(removeBall) withObject:nil afterDelay:1];
break;
}
default:
{
break;
}
}
}
现在在我的perform selector(startTimer:)
我得到了在NSPoint
触摸的点的坐标(因为我正在使用NSDictionary
),我想知道的是..我们如何将这些点转换为CGPoints。
知道我该怎么做吗?
任何帮助将不胜感激。
如果您使用的是NSValue
,则可以从使用CGPointValue
那个获得CGPoint
。
例如
NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithCGPoint:location] forKey:@"location"];
CGPoint points = [[touchloc valueForKey:@"location"] CGPointValue];
CGPointCreateDictionaryRepresentation
和CGPointMakeWithDictionaryRepresentation
文档说NSPoint是:
typedef struct _NSPoint {
CGFloat x;
CGFloat y;
} NSPoint;
CGPoint是:
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
因此它们是等效的结构,您应该能够简单地将其从一个转换为另一个。 两者都不是对象类型,因此您不能将两者直接存储在字典中,而必须将它们包装在NSValue中才能将它们变成对象。 您可能正在从字典中获取NSValue对象,并且NSValue具有这两种类型的访问器,因此无需进行强制转换即可直接获取所需的对象:
CGPoint a = [somePoint CGPointValue];
NSPoint b = [somePoint pointValue];
对我来说,将cgpoint保存并返回到nsdictionary的正确函数如下:
NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithPoint:cgPointPositionTouchPoint] forKey:@"location"];
CGPoint points = [[touchloc valueForKey:@"location"] pointValue];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.