ios - Swift 中令人困惑的反向触摸事件

标签 ios swift sprite-kit touchesbegan

在我的函数中

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

我得到了反转的 Y 值。如果我点击顶部,我会得到〜0值,如果我点击底部,我会得到高值。这使得我的 Sprite 移动到错误的 Y 方向。

但是,如果我删除 self.view 中的“.view”,它就会正常工作。有谁知道为什么当我使用 self.view 时它会颠倒?

最佳答案

正如 Whirlwind 所解释的在您的问题下面的评论中,发生这种情况是因为使用 self 您指示 SKScene (SpriteKit),而使用 self.view 您使用 SKView (UIView UIKit 系统的子类):

iOSUIKit坐标系的原点位于绘图区域的左上角,正值从该位置向下和向右延伸。

SpriteKit 坐标系在 iOSOS X 上使用相同的坐标系,原点 (0,0) 位于下方- 左上角,(1334,750) 坐标在右上角。

关于ios - Swift 中令人困惑的反向触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41172551/

相关文章:

ios - 如何从 Sprite 节点中删除特定的 child ?

ios - 将事件监听器添加到基于回合的 ios 游戏时发出警告

ios - NSBundle mainBundle pathForResource : Not working unless @2x included

ios - swift 中的 DatePicker 问题

ios - 如何在 Swift/iOS 中将静态 TableView Controller 单元格限制到安全区域

swift - NSData编码

ios - Swift 中的 CATransform3DMakeRotation anchor 和位置

ios - 如何在我的 NSXMLParser 中实现此方法来提取图像

ios - iOS 配置门户中的开发证书是什么?

ios - Xcode 7 Sprite Kit X坐标在iOS模拟器中偏离中心