Confusing reversed touch event in Swift

天大地大妈咪最大 提交于 2019-12-24 00:57:50

问题


In my function

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
    for touch in touches{
        let touchLocation = touch.location(in: self.view)
        print("X: \(touchLocation.x) Y: \(touchLocation.y)")
    }
}

I get reversed Y value. If I click at the top I get ~0 value and if I click at the bottom I get high value. This makes my sprite im moving move in the wrong Y direction.

However if I remove '.view' in self.view it works as it should. Does anyone know why it's reversed when I use self.view?


回答1:


As explained by Whirlwind in the comment below your question, this happened because with self you indicate SKScene (SpriteKit) and with self.view you use SKView (UIView subclass so UIKit system):

UIKit coordinate system for iOS has its origin at the upper left of the drawing area, and positive values extend down and to the right from it.

SpriteKit coordinate system uses the same coordinate system on both iOS and OS X, has the origin (0,0) in the lower-left corner, and the (1334,750) coordinate in the upper-right corner.



来源:https://stackoverflow.com/questions/41172551/confusing-reversed-touch-event-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!