的NSDictionary到CGPoint(NSDictionary to CGPoint)

2019-09-17 11:52发布

我工作的一个游戏,其中我试图分别若单水龙头,双击子弹射击两种不同类型。

继承人我在我触摸我开始做的方法:

- (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 ..?

任何想法,我该怎么办呢?

任何帮助将真正的赞赏。

Answer 1:

如果你正在使用NSValue你可以得到CGPoint从使用回CGPointValue

NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithCGPoint:location] forKey:@"location"];
    CGPoint points = [[touchloc valueForKey:@"location"] CGPointValue];


Answer 2:

CGPointCreateDictionaryRepresentationCGPointMakeWithDictionaryRepresentation



Answer 3:

该文档说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];    


Answer 4:

对我来说,为保存并返回到cgpoint NSDictionary的正确的功能如下:

NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithPoint:cgPointPositionTouchPoint] forKey:@"location"];

CGPoint points = [[touchloc valueForKey:@"location"] pointValue];


文章来源: NSDictionary to CGPoint