convertPoint:fromView:在模拟器上运行良好,而不是在设备上运行

我很高兴看到这个: 答案如何转换子视图的点,它在模拟器上完美的工作。

但由于某种原因,它不能在设备上工作…这可能是视图的位置是不同的设备pipe理 – 或者它只是另一个谜? 或者(希望)我写错了,你们可以帮助… 🙂

这是我的路线:

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

我有一个不同的问题,涉及到这个问题,并已经注意到,我不得不考虑在转换矩形屏幕的规模。 (即它在模拟器上工作,不是因为它是模拟器,而是因为模拟器处于非视网膜模式)

 // 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)); 

好。 这是%d和%f之间差别的一个教训:)显然它是完美的。

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

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