【问题标题】:convertPoint:fromView: works well on the simulator, not on deviceconvertPoint:fromView: 在模拟器上运行良好,而不是在设备上运行
【发布时间】:2011-05-16 06:13:54
【问题描述】:

我很高兴看到这个:answer for how to convert point of a subview 它在模拟器上非常适合我。

但由于某种原因,它无法在设备上运行... 可能是设备对视图位置的管理方式不同 - 还是只是另一个谜?或者(希望)我写错了,你们可以帮忙...... :)

这是我的台词:

CGPoint yoel = [imagePressed.imageView convertPoint:imagePressed.frame.origin toView:nil];

【问题讨论】:

    标签: ios uiview cgpoint


    【解决方案1】:

    我有一个与此问题相关的不同问题,并且注意到在转换矩形时我必须考虑屏幕的比例。 (即它在模拟器上工作不是因为它是模拟器,而是因为模拟器处于非视网膜模式)

    // the following convertRect is like asking a view: "what would one of your subviews (aView) have for a frame in my (toView) coordinate space if it were to become my subview?
        _originalFrame = [[aView superview] convertRect: aView.frame toView: self];
    
        CGFloat scale = [[UIScreen mainScreen] scale];
    
        _originalFrame.origin.x /= scale;
        _originalFrame.origin.y /= scale;
        _originalFrame.size.width /= scale;
        _originalFrame.size.height /= scale;
    
        NSLog(@"Converted Frame: %@", NSStringFromCGRect(_originalFrame));
    

    【讨论】:

    • 我也注意到了,用CGPoint 而不是CGRect,你的答案是正确的。但我想知道为什么会发生这种情况,也许我们的方法有问题?
    【解决方案2】:

    好的。这是 %d 和 %f 之间区别的一个教训 :) 显然它工作得很好。

    我的错误是-我在模拟器上 DLog(@"yoel is %f", yoel.x); DLog(@"yoel is %f", yoel.y); 就在设备上运行它之前,我将其更改为 DLog(@"yoel is %d", yoel.x); DLog(@"yoel is %d", yoel.y); 由于 CGPoint 是浮动的,所以我得到了 0 而不是正确的坐标...

    另一个教训 - 在我将测试从模拟器切换到设备之前,我永远不会更改代码,所以不要责怪苹果而是我自己 :)

    【讨论】:

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