代码之家  ›  专栏  ›  技术社区  ›  Jad Ghadry

iPad的3D触控等效

  •  0
  • Jad Ghadry  · 技术社区  · 6 年前

    我尝试用swift开发一款游戏,它利用了iphone的3D触摸硬件。但是,当我向应用商店提交我的应用时,它被拒绝了,因为游戏 无法在iPad上播放 .

    我的问题是,为非3D触摸设备实现类似功能的最佳方法是什么?我现在的方法是通过实现以下方法

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    
        if self.didStartGame, let touch = touches.first {
    
            let maximumPossibleForce = touch.maximumPossibleForce
            let force = touch.force
            let normalizedForce = min(force/maximumPossibleForce * 2.5, 1)
    
            // Added game-related code here to modify scene accordingly
    
        }
    
    }
    

    在非3D触摸设备上运行后者时,调试 touch.maximumPossibleForce 回报 0 .

    1 回复  |  直到 6 年前
        1
  •  2
  •   joern    6 年前

    在不支持强制触摸的设备上无法检测到强制触摸。

    但也许你可以用 majorRadius 上的属性 UITouch . 它给出了接触的半径。

    使用半径,您可以让没有3d触控设备的用户通过手指的角度控制您的游戏:

    enter image description here

    这是上述示例的代码:

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let pseudoForce = touches.first?.majorRadius else { return }
        label.text = "\(pseudoForce)"
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let pseudoForce = touches.first?.majorRadius else { return }
        label.text = "\(pseudoForce)"
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        label.text = "-"
    }