RealityKit 的 ARView – 多次触摸不起作用

问题描述 投票:0回答:1

我需要使用函数 TouchMoved 检测两个手指。该代码将打印出脚趾数。该程序适用于普通 UIView(打印 Touchs.Count=2),但不适用于 ARView(始终打印 Touchs.Count=1)。

有什么解决办法吗?

import UIKit
import RealityKit

class GameViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.isMultipleTouchEnabled = true

//        // This code works for detecting two fingers
//        let v = UIView(frame: view.frame)
//        v.backgroundColor = .green
//        view.addSubview(v)
//        v.isMultipleTouchEnabled = true

        // This code doesn't work for detecting two fingers
        let arView = ARView(frame: view.frame,
                            cameraMode: .nonAR,
                            automaticallyConfigureSession: true)
        view.addSubview(arView)
        arView.isMultipleTouchEnabled = true        
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touchesBegan, touches.count=\(touches.count)")
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touchesMoved, touches.count=\(touches.count)")
    }

}
swift uikit multi-touch realitykit arview
1个回答
0
投票

以下代码适用于任何 UIKit 视图,包括 arView。

import UIKit
import RealityKit

class ViewController : UIViewController {
    @IBOutlet var arView: ARView!
    var fingers = [UITouch?](repeating: nil, count: 50)
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            for (i, finger) in fingers.enumerated() {
                if finger == nil {
                    fingers[i] = touch
                    print("\(i + 1) touches")
                    break
                }
            }
        }
    }
}

结果:

1 touches
2 touches
3 touches
etc.
© www.soinside.com 2019 - 2024. All rights reserved.