2016-11-20 25 views
0

你能否給我一種獲得在所有iOS設備中相同點的座標的方法?獲取點的座標而不考慮分辨率

我在文章Coordinate Systems and Transforms發現

用戶在可可座標空間是您用於所有繪圖命令的環境。它表示一個固定的縮放座標空間,這意味着您在此空間中發出的繪圖命令會生成大小一致的圖形,而與基礎設備的分辨率無關。

我試圖做到這一點是這樣的:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) 
{ 
    if let touch = touches.first{ 
     let lastPoint = touch.location(in: self.view.superview) 
     let precisePoint = graphView.convert(lastPoint, to: self.view.superview) 
    } 
} 

但lastPoint和precisePoint取決於所使用的設備上。

感謝您的幫助

+0

好了,lastpoint將相對於self.views座標,而你的precisePoint將使用lastPoint它是相對於graphView和將該位置轉換爲與self.view相關的位置,因此難怪這些點不匹配。唯一的情況是我看到兩點是相同的,如果self.view和graphView是完全相同的大小。我建議你重新考慮你的積分轉換。此外,這將有助於瞭解你計算兩點後的意圖。 – DerrickHo328

+2

您是否嘗試過在屏幕邊界上劃分點?這應該給你一個相對的點,然後每次在屏幕上顯示它的時候乘以屏幕邊界。 – Rikh

+0

@ Calimari328我知道這兩點會有所不同。我只是試圖使用[鏈接](https://developer.apple.com/reference/uikit/uicoordinatespace) – Java

回答

0

得益於以下@Rikh答案:

var points = [CGPoint]() 

//MARK: Touch events 
override func touchesBegan(_ touches: Set<UITouch>, 
          with event: UIEvent?){ 
    if let touch = touches.first{ 
     let lastPoint = touch.location(in: self.view.superview) 

     let bounds = UIScreen.main.bounds 

     points.append(CGPoint(x: lastPoint.x/bounds.maxX, y: lastPoint.y/bounds.maxY)) 
    } 
} 


func convertPointsToView(){ 
    let bounds = UIScreen.main.bounds 

    for point in points{ 
     let convertedPoint = CGPoint(x: point.x * bounds.maxX, y: point.y * bounds.maxY) 

     print(convertedPoint) 
    } 
}