【问题标题】:iOS: [X convertPoint: somePoint toView: nil]; FAILS to return window-coordsiOS:[X convertPoint:somePoint toView:nil];无法返回窗口坐标
【发布时间】:2011-05-14 19:59:23
【问题描述】:

我正在编写一个方法,它应该接收一个点:'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}

【问题讨论】:

  • 你可能会看到奇怪的行为是 target 是 nil,因为在 nil 上调用结构返回方法的返回值是未定义的。

标签: iphone ios view coordinates


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    为什么不尝试替代方案,看看效果如何?

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多