开发者

iOS: [X convertPoint: somePoint toView: nil]; FAILS to return window-coords

I'm writing a method which should receive a point: 'in_centre', which will be in the coordinate system of the current view ( also passed as another parameter: 'target' )

I need to convert this point into window coordinates.

The documentation says I should do this by setting the second parameter here to nil:

centerPoint = [target convertPoint:in_centre toView:nil];  // get in Window Coords
NSLog(@"passed in (vc): %@", NSStringFromCGPoint(in_centre));
NSLog(@"centerPoint_wc: %@", NSStringFromCGPoint(centerPoint));

But the output shows that it is not doing anything.

2010-11-24 07:32:45.815 ChordWheel[3195:207] passed in (vc): {150, 150}

开发者_如何学编程2010-11-24 07:32:45.816 ChordWheel[3195:207] centerPoint_wc: {150, 150}

This is wrong -- it should be the centre of the iPhone screen: {160, 240}

What is going on?

EDIT: thank you for those suggestions –- I tried them and still no joy:

- (id) initWithFrame: (CGRect) theFrame
              target: (id) p_target
     actionPlayChord: (SEL) p_actionPlayChord
      actionSettings: (SEL) p_actionSettingsClick
{
    target = p_target;
    actionPlayChord = p_actionPlayChord;

    self = [super initWithFrame: theFrame];

    if ( ! self )
        return nil;

    CGPoint centre = CGPointMake(theFrame.size.width / 2.0, 
                    theFrame.size.height / 2.0);

    CGPoint c_wc = [self convertPoint: centre toView: nil];
    CGPoint c_wc2 = [self convertPoint: centre toView: self.window];

    NSLog(NSStringFromCGRect(theFrame));
    NSLog(NSStringFromCGPoint(centre));
    NSLog(NSStringFromCGPoint(c_wc));
    NSLog(NSStringFromCGPoint(c_wc2));

2010-11-24 14:33:55.202 ChordWheel[3452:207] {{10, 90}, {300, 300}}

2010-11-24 14:33:55.202 ChordWheel[3452:207] {150, 150}

2010-11-24 14:33:55.203 ChordWheel[3452:207] {150, 150}

2010-11-24 14:33:55.204 ChordWheel[3452:207] {150, 150}


It worked for me the last time I checked, but it may have changed between OS releases. You can use [v convertPoint:p toView:v.window] (a window is a view), but that might return something different for windows which aren't full-screen windows ("window base coordinates" might be relative to the screen the window is in, for example, instead of relative to the top-left corner of the window).


Why not try the alternatives, and see how they work out?

[target convertPoint:in_centre toView:target.window]
[target.window convertPoint:in_centre fromView:target]

And maybe the coordinates you're getting are actually the correct coords in the base window, but they're just not the ones you'd like.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜