gpt4 book ai didi

ios - 快速获取屏幕范围内的触摸位置?

转载 作者:搜寻专家 更新时间:2023-11-01 06:25:48 25 4
gpt4 key购买 nike

我有一个比设备屏幕尺寸大的场景,并且有一个摄像头在场景周围跟随玩家。如果用户触摸屏幕右侧,我想将播放器向右移动;如果他们触摸屏幕左侧,我想将播放器移动到左侧。但是由于我的场景比我的手机屏幕大,在 touchesBegan 方法中,当用户到达场景的最远边缘时会出现问题。如果玩家走到场景的左侧,即使用户触摸屏幕的右侧,它仍然会被注册为“左触摸”,因为整个手机屏幕只被场景的左侧填满.

这是我的 touchesBegan 方法中的内容:

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

let touch = touches.first
if let location = touch?.location(in: self) {
print(location)

if location.x < 0 {
player.physicsBody?.applyImpulse(CGVector(dx: -8, dy: 0))
} else {
player.physicsBody?.applyImpulse(CGVector(dx: 8, dy: 0))
}
}
}

我尝试更改 touch?.location(in: self) 部分,但这似乎没有效果。有没有办法记录用户触摸相对于屏幕边界的位置,而不是场景?

最佳答案

您可以使用 UIScreen.main.focusedView 而不是 self

关于ios - 快速获取屏幕范围内的触摸位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55085429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com