I am trying to create a button on an ARKit project, so I created a plane on my sceneView on an ARKit project, to which I assigned a SKScene containing a button as material.
The button is a custom SKNode that detects touches to change color and call an action, however, even though I can detect touches on the SCNNode, I cannot detect them in the SKNode button.
The Node is placed this way:
func getButtonNode(_ title: String, origin: SCNVector3, moveTo: SCNVector3? = nil) {
guard let _ = self.sceneView.session.currentFrame else {
print("No current frame")
return
}
let nodeHeight = imageReference!.physicalSize.height
let sk = SKScene(size: CGSize(width: 400, height: 100))
sk.backgroundColor = UIColor.clear
let button = ButtonNode(text: " " + title + " ")
button.name = "button"
button.setBackgroundColor(.white)
button.setClickedTarget(self, action: #selector(tapped))
sk.addChild(button)
let planeGeometry = SCNPlane(width: nodeHeight, height: nodeHeight/4)
planeGeometry.firstMaterial?.diffuse.contents = sk
planeGeometry.firstMaterial?.isDoubleSided = true
let planeNode = SCNNode()
planeNode.eulerAngles.x = .pi / 2
planeNode.geometry = planeGeometry
planeNode.position = origin
if let finalPosition = moveTo {
planeNode.runAction(.move(to: finalPosition, duration: 0.5))
}
planeNode.name = "ButtonNode"
self.node!.addChildNode(planeNode)
}
and part of the button code, which I got from apple is:
init(text txt: String) {
super.init()
// create a label
lbl = SKLabelNode(text: txt)
lbl!.horizontalAlignmentMode = .center
lbl!.fontSize = 50
lbl!.numberOfLines = 1
lbl!.fontColor = UIColor.darkGray
lbl!.position = CGPoint(x: CGFloat(200.0), y: CGFloat(50.0))
lbl!.preferredMaxLayoutWidth = 290
lbl!.horizontalAlignmentMode = .center
lbl!.verticalAlignmentMode = .center
// create the background
size = CGSize(width: CGFloat(lbl!.frame.size.width + 30.0), height: CGFloat(90.0))
background = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 400, height: 100), cornerRadius: 50)
background!.fillColor = .white
background!.lineWidth = 0
// add to the root node
addChild(background!)
addChild(lbl!)
// Track mouse event
isUserInteractionEnabled = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
setBackgroundColor(UIColor.white.withAlphaComponent(0.5))
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
setBackgroundColor(UIColor.white.withAlphaComponent(1))
_ = (targetClicked! as AnyObject).perform(actionClicked, with: self)
}
I suspect setting a SKNode as material somehow eliminates is gesture recognition, but when I assign UI Elements like a UIWebView, I don't seem to have a problem with gestures.
Any ideas of how I could detect touches on my button?
Thanks in advance.