RealityKit's ARView – Multiple touches not working

96 views Asked by At

I need to detect two fingers with function touchesMoved. The code will print out the number of toches. The program works well with normal UIView (prints touches.count=2) but not with ARView (always prints touches.count=1).

Any solution?

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

}
1

There are 1 answers

0
Andy Jazz On

The following code works with any UIKit's view, including 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
                }
            }
        }
    }
}

Results:

1 touches
2 touches
3 touches
etc.