iphone - iOS: [X convertPoint: somePoint toView: nil];无法返回窗口坐标

标签 iphone ios view coordinates

我正在编写一个方法,它应该接收一个点:'in_centre',它将在当前 View 的坐标系中(也作为另一个参数传递:'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 弦轮 [3452:207] {150, 150}
2010-11-24 14:33:55.203 弦轮 [3452:207] {150, 150}
2010-11-24 14:33:55.204 弦轮 [3452:207] {150, 150}

最佳答案

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

关于iphone - iOS: [X convertPoint: somePoint toView: nil];无法返回窗口坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4262501/

相关文章:

asp.net-mvc-3 - 在 asp.net mvc 中以字符串形式获取 View 以发送电子邮件

android - 将 subview 添加到两个布局(多个父级)

html - jEditable TextArea,我需要在保存时保持格式,但在编辑时显示用户友好的文本(MVC3)

iOS 7 : Custom Style Cell in Tableview: How to reference labels?

iphone - 将带有两个 UIBarButtonItem 的 UIToolbar 添加到 UINavigationBar : poor UIToolbar and what about iPhone 4

iphone - iOS 主机公共(public) url

iphone - UIViewController 子类支持 NIB 加载和非

ios - UITextField 切断了文本的右侧,并且没有在正确的位置显示光标

iphone - RTSPPlay 不运行

ios - 没有完整引用的 Firebase 查询 2 级深度