2017-02-28 8 views
1

SKScene에서 사용자가 화면의 왼쪽 또는 오른쪽을 터치하고 있는지 여부를 감지하려고합니다.화면의 왼쪽과 오른쪽의 터치를 신속하게 감지합니다. SKScene

다음 코드를 함께 넣었지만 터치 한 위치와 상관없이 "오른쪽"만 출력합니다. 작동하는 것 같다

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 



    for touch in touches { 
     let location = touch.location(in: self) 

     if(location.x > self.frame.size.width/2){ 
      print("Left") 
     } 

     else if(location.x < self.frame.size.width/2){ 
      print("Right") 
     } 
    } 
} 
+0

오른쪽 (>)보다 작고 왼쪽 (<)보다 작지 않습니까? –

+0

맞아 .. 어디서나 터치하면 왼쪽으로 출력됩니다. –

답변

0
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 

    for touch in touches { 
     let location = touch.location(in: self) 

     if(location.x < 0){ 
      print("Left") 
     } 
     else { 
      print("Right") 
     } 
    } 
} 

. 터치가 화면의 왼쪽/오른쪽에 있는지 여부를 확인하기 전에 항상 올바른 것을주었습니다. 예를 들어, iPhone 7 plus에서는 터치가 (x가 20이라고 가정 해 봅시다) 365 왼쪽의 오른쪽에 있는지 확인합니다. 20이 365보다 작 으면 오른쪽을 클릭했다.