ios - 快速检测屏幕左右两侧的触摸 SKScene

标签 ios swift skscene touches

我正在尝试检测用户是否在 SKScene 中触摸屏幕的左侧或右侧。

我已将以下代码放在一起,但无论触摸何处,它都仅输出“Right”。

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

最佳答案

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,因此表示您单击了右侧。

关于ios - 快速检测屏幕左右两侧的触摸 SKScene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42498047/

相关文章:

ios - addSubview 将我的 View 设置为默认

iOS swift : Pushing two view controllers when app launched from push notification

ios - Swift 可选链在闭包中不起作用

ios - 我的项目是否需要 SpriteKit 场景 (.sks) 文件?

ios - SpriteKit : why does SKView disappear at one height but appears at another height? SKView 的最大高度是多少?

iOS 获得不同于编程的其他颜色

iOS应用程序: How to localize the app name (for Australia English) displayed on the device?

ios - 带有 Firebase 数据库的 Swift 3 照片共享扩展在首次发送后不起作用

ios - 从 SKScene 呈现后无法关闭 View Controller

ios - 我怎样才能确保我的 guard 语句不会以 nil 值继续?