2011-05-16 130 views
0

我很高兴看到这个:answer for how to convert point of a subview 它在模拟器上对我来说非常完美。convertPoint:fromView:在模拟器上运行良好,不在设备上

但由于某种原因,它不能在设备上工作... 它可能是视图的位置是不同的设备管理 - 或者它只是另一个谜?或者,(希望)我错误地写它,你们可以帮助... :)

这里是我的路线:

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

回答

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,而不是正确的坐标...

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

8

我有一个不同的问题,涉及到这个问题,并已注意到,我不得不考虑转换矩形时的屏幕比例。 (即它的工作就不是因为它的仿真模拟器,但由于模拟器在非视网膜模式)

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

我注意到,以及,用'CGPoint',而不是'CGRect'和你的回答是正确的修复。但我想知道为什么会发生这种情况,或许我们的方法有问题? – mokagio 2012-11-01 15:54:01

相关问题