iOS:[X convertPoint:somePoint toView:nil];失败返回窗口坐标

时间:2010-11-24 00:40:44

标签: iphone ios view coordinates

我正在写一个应该接收一个点的方法:'in_centre',它将在当前视图的坐标系中(也作为另一个参数传递:'target')

我需要将此点转换为窗口坐标。

文档说我应该通过将第二个参数设置为nil来实现:

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

但输出显示它没有做任何事情。

2010-11-24 07:32:45.815 ChordWheel [3195:207]传入(vc):{150,150}
  2010-11-24 07:32:45.816 ChordWheel [3195:207] centerPoint_wc:{150,150}

这是错误的 - 它应该是iPhone屏幕的中心:{160,240}

发生了什么事?

编辑:谢谢你们的建议 - 我试过了,但仍然没有快乐:

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

2 个答案:

答案 0 :(得分:0)

我最后一次检查它对我有用,但它可能在OS版本之间发生了变化。您可以使用[v convertPoint:p toView:v.window](窗口是视图),但对于不是全屏窗口的窗口可能会返回不同的内容(“窗口基座坐标”可能与窗口所在的屏幕相关,对于例如,而不是相对于窗口的左上角。)

答案 1 :(得分:0)

为什么不尝试替代方案,看看它们是如何运作的?

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

也许您获得的坐标实际上是基本窗口中的正确坐标,但它们不是您想要的坐标。