NSDictionary to CGPoint

2019-06-11 08:19发布

I am working on a game, in which i am trying to fire two different types if bullets on single tap and double tap respectively.

Heres what I am doing in my touch began method:

- (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;
            }
        }
  }

Now in my perform selector(startTimer:) I get the co-ordinates of point where I touch in NSPoint (As I am using NSDictionary) All I want to know is.. how we can convert those points to CGPoints.. ?

Any idea how can I do it?

Any help will be truly appreciated.

4条回答
手持菜刀,她持情操
2楼-- · 2019-06-11 09:04

For me, the correct function for save and return cgpoint to nsdictionary is is the following:

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

CGPoint points = [[touchloc valueForKey:@"location"] pointValue];
查看更多
太酷不给撩
3楼-- · 2019-06-11 09:08

if you are using NSValue you can get CGPoint back from that using CGPointValue.

e.g.

NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithCGPoint:location] forKey:@"location"];
    CGPoint points = [[touchloc valueForKey:@"location"] CGPointValue];
查看更多
神经病院院长
4楼-- · 2019-06-11 09:15

CGPointCreateDictionaryRepresentation and CGPointMakeWithDictionaryRepresentation

查看更多
Ridiculous、
5楼-- · 2019-06-11 09:15

The docs say that NSPoint is:

typedef struct _NSPoint {
    CGFloat x;
    CGFloat y;
} NSPoint;

and CGPoint is:

struct CGPoint {
     CGFloat x;
    CGFloat y;
};
typedef struct CGPoint CGPoint;

so they're equivalent structs, and you should be able to simply cast to convert from one to the other. Neither is an object type, so you can't store either in a dictionary directly but have to box them up in a NSValue to turn them into objects. You're probably getting NSValue objects from your dictionary, and NSValue has accessors for both types, so you can get the one you need directly without a cast:

CGPoint a = [somePoint CGPointValue];
NSPoint b = [somePoint pointValue];    
查看更多
登录 后发表回答