2016-11-20 3 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는 사용되는 장치에 따라 달라집니다

나는 이런 식으로이 일을 시도했다. 아래 @Rikh 답변에 도움을

덕분에

+0

음, lastpoint은 precisePoint이는 graphView와 상대적입니다 lastPoint를 사용합니다 좌표 동안 self.views에 상대적 것 그 위치를 self.view와 관련된 위치로 변환하면 포인트가 일치하지 않는 것은 당연한 일입니다. 유일한 두 가지 점은 self.view와 graphView가 정확히 같은 크기인지 아닌지입니다. 포인트 전환을 다시 생각해 보시기 바랍니다. 또한 두 가지 점을 계산할 때 의도가 무엇인지 알면 도움이됩니다. – DerrickHo328

+2

포인트를 화면 경계로 나누어 보았습니까? 그것은 당신에게 상대적인 포인트를 주어야하고, 그 포인트를 디스플레이하기 전에 스크린 경계로 곱해야합니다. – Rikh

+0

@ Calimari328이 두 가지 점이 다르다는 것을 알고 있습니다. 나는 방금 [링크] (https://developer.apple.com/reference/uikit/uicoordinatespace) – Java

답변

0

감사 :

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) 
    } 
}