我试图在ARKit项目上创建一个按钮,所以我在ARKit项目上的sceneView上创建了一个平面,并为其分配了一个包含按钮作为材质的SKScene。
该按钮是自定义SKNode,可检测触摸以更改颜色并调用动作,但是,即使我可以检测到SCNNode上的触摸,也无法在SKNode按钮中检测到它们。
以这种方式放置节点:
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)
}
和从苹果公司获得的部分按钮代码是:
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)
}
我怀疑将SKNode设置为某种材料可以以某种方式消除手势识别,但是当我分配UIWebView之类的UI元素时,手势似乎没有问题。
关于如何检测按钮上的触摸的任何想法?
谢谢。