我想限制点击次数

I want to limit the number of taps

每次点击都会在我的场景中添加一个对象,但我只想添加一次,然后禁用点击手势。我到处查看,但其中 none 正在使用我的代码。有人可以帮我弄这个吗?将水龙头限制为仅 1 或禁用它。我尝试将点击手势添加为 Outlet,然后设置 .isEnabled = false 但它仍然无法正常工作。

class ARScene: UIViewController, ARSCNViewDelegate, UIGestureRecognizerDelegate {

@IBOutlet weak var sceneView: ARSCNView!




var tap : UITapGestureRecognizer!

override func viewDidLoad() {
    super.viewDidLoad()
   self.sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints, ARSCNDebugOptions.showWorldOrigin]
    let configuration = ARWorldTrackingConfiguration()

    self.sceneView.session.run(configuration)

    // Set the view's delegate
    sceneView.delegate = self

    sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints]

    // Show statistics such as fps and timing information
    sceneView.showsStatistics = true

    // Create a new scene
    // let scene = SCNScene(named: "art.scnassets/ship.scn")!

    // Set the scene to the view
    //sceneView.scene = scene

    registerGestureRecognizer()



}

func registerGestureRecognizer(){
    let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
    tap.numberOfTapsRequired = 1
    sceneView.addGestureRecognizer(tap)

}



@objc func handleTap(gestureRecognizer: UIGestureRecognizer){

    //let touchLocation = gestureRecognizer.location(in: sceneView)
    let sceneLocation = gestureRecognizer.view as! ARSCNView
    let touchLocation = gestureRecognizer.location(in: sceneLocation)
    let hitResult = self.sceneView.hitTest(touchLocation, types: [.existingPlaneUsingExtent, .estimatedHorizontalPlane])

    if hitResult.count > 0 {

        guard let hitTestResult = hitResult.first else{
            return
        }


        let node = SCNNode()
        let scene = SCNScene(named: "art.scnassets/bucket/Bucket2.scn")


        let nodeArray = scene!.rootNode.childNodes

        for childNode in nodeArray{

            node.addChildNode(childNode as SCNNode)
        }


        let worldPos = hitTestResult.worldTransform
        node.scale = SCNVector3Make(0.009,0.009,0.009);
        node.position = SCNVector3(x: 0, y: worldPos.columns.3.y, z: -1.4)

        sceneView.scene.rootNode.addChildNode(node)
        tap.isEnabled = false //NOT WORKING, I want to stop tap gesture here

    }

}

为了禁用您的 tapGesture,您需要引用您分配点击的同一手势实例。 有两种方法可以创建全局实例,这样您就可以从任何地方更改其属性,例如 Enable/Disable 或者从 action/method 访问该手势的变量。

为您的点击创建全局变量 喜欢

var tap: UITapGestureRecognizer! // global varibale

func registerGestureRecognizer(){
   tap = UITapGestureRecognizer(target: self, action: #selector(handlePanGesture(_:))) 
   tap.numberOfTapsRequired = 1
   sceneView.addGestureRecognizer(tap)
}

然后在taphandle中禁用它

tap.isEnabled = false // disable the gesture

2 将您的句柄方法更新为

@objc func handleTap(withTapRecognizer tapGesture: UITapGestureRecognizer) {
  ........
   tapGesture.isEnabled = false // use the instance variable and disable the gesture this will disable the gesture from which its been called
}

为了在函数中访问手势,您需要使用

对其进行初始化
    let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(gestureRecognizer:)))

然后在您的 handleTap 方法中,您可以在函数末尾执行此操作

    gestureRecognizer.view?.removeGestureRecognizer(gestureRecognizer)