2011-05-16 133 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

相關問題