每次轻按都会在我的场景中添加一个对象,但是我只想添加一次,然后禁用轻按手势。我到处都看过,但是没有人正在使用我的代码。有人可以帮我弄这个吗?将抽头限制为1或禁用它。我尝试将点击手势添加为“插座”,然后设置.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
}
}
答案 0 :(得分:1)
要禁用tapGesture,您需要引用分配了tap的同一手势实例。 可以通过两种方式创建全局实例,以便可以在任何地方更改其属性,例如启用/禁用 或从动作/方法中访问该手势的变量。
为您的水龙头创建全局变量 像
var tap: UITapGestureRecognizer! // global varibale
func registerGestureRecognizer(){
tap = UITapGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
tap.numberOfTapsRequired = 1
sceneView.addGestureRecognizer(tap)
}
然后在轻触手柄中禁用它
tap.isEnabled = false // disable the gesture
或
2将您的handle方法更新为
@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
}
答案 1 :(得分:0)
要访问该功能中的手势,您需要使用对其进行初始化
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(gestureRecognizer:)))
然后在您的handleTap方法中,您可以在函数末尾
gestureRecognizer.view?.removeGestureRecognizer(gestureRecognizer)