在Swift和SpriteKit中,您可以将对象指定为"isUserInteractionEnabled=false“。令人困惑的是,这并不意味着对象将忽略触摸并让这些触摸通过它下面的节点。
我构建SpriteKit应用程序的方式是创建一个节点(比如SKShapeNode或SKSpriteNode),然后给它贴上标签,这对我来说是很常见的。我想要的是标签忽略触摸,并让这些触摸通过它后面的节点,但如果"isUserInteractionEnable“不是票证,我不能理解这是如何做到的。
有什么建议吗?下面是一个示例标签,它接受触摸,但不应该:
class MenuButton: SKShapeNode {
/* The MenuBar will have as children nine buttons of two types. Dialog Springers and SubMenu Droppers */
// MARK: - Class Variables
var title = "Button"
var menu = SKShapeNode()
let buttonLabel = SKLabelNode(fontNamed: devFont4)
init(title: String)
{
super.init()
isUserInteractionEnabled = true
self.title = title
self.name = "\(title) Menu Button"
let btnLen = title.count * 10 + 16
let menuSize = CGSize(width: btnLen, height: 32)
menu = SKShapeNode(rectOf: menuSize)
addChild(menu)
menu.fillColor = .clear
menu.strokeColor = .clear
menu.lineWidth = 0
menu.zPosition = 501
// Add Label
let letterHeight: CGFloat = 22
buttonLabel.text = title
buttonLabel.name = "\(title) Label"
buttonLabel.fontSize = letterHeight
buttonLabel.fontColor = .black
buttonLabel.zPosition = 502
buttonLabel.position = CGPoint(x: 0, y: Int(-letterHeight/2) + 2)
buttonLabel.isUserInteractionEnabled = false
addChild(buttonLabel)
}
}
发布于 2019-03-26 02:10:06
只有一个第一响应者接收触摸事件。isUserInteractionEnabled
用于决定先调用哪一个,即使两个都已启用。播放下面的代码,你就能感觉到发生了什么。
class Button: SKLabelNode{
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("button")
}
}
class MenuButton: SKShapeNode {
/* The MenuBar will have as children nine buttons of two types. Dialog Springers and SubMenu Droppers */
// MARK: - Class Variables
var title = "Button"
var menu = SKShapeNode()
let buttonLabel = Button.init()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("hit")
}
init(title: String)
{
super.init()
isUserInteractionEnabled = true
self.title = title
self.name = "\(title) Menu Button"
let btnLen = title.count * 10 + 16
let menuSize = CGSize(width: btnLen, height: 32)
menu = SKShapeNode(rectOf: menuSize)
addChild(menu)
menu.fillColor = .green
menu.strokeColor = .clear
menu.lineWidth = 0
menu.zPosition = 501
menu.isUserInteractionEnabled = false
// Add Label
let letterHeight: CGFloat = 122
buttonLabel.text = title
buttonLabel.name = "\(title) Label"
buttonLabel.fontSize = letterHeight
buttonLabel.fontColor = .black
buttonLabel.zPosition = 502
buttonLabel.position = CGPoint(x: 0, y: Int(-letterHeight/2) + 2)
buttonLabel.isUserInteractionEnabled = true
addChild(buttonLabel)
self.path = UIBezierPath.init(rect:self.calculateAccumulatedFrame()).cgPath
self.fillColor = UIColor.red
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
https://stackoverflow.com/questions/55342812
复制相似问题