首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使SKLabelNode忽略所涉及的内容

使SKLabelNode忽略所涉及的内容
EN

Stack Overflow用户
提问于 2019-03-26 00:53:11
回答 1查看 192关注 0票数 1

在Swift和SpriteKit中,您可以将对象指定为"isUserInteractionEnabled=false“。令人困惑的是,这并不意味着对象将忽略触摸并让这些触摸通过它下面的节点。

我构建SpriteKit应用程序的方式是创建一个节点(比如SKShapeNode或SKSpriteNode),然后给它贴上标签,这对我来说是很常见的。我想要的是标签忽略触摸,并让这些触摸通过它后面的节点,但如果"isUserInteractionEnable“不是票证,我不能理解这是如何做到的。

有什么建议吗?下面是一个示例标签,它接受触摸,但不应该:

代码语言:javascript
复制
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)

    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-03-26 02:10:06

只有一个第一响应者接收触摸事件。isUserInteractionEnabled用于决定先调用哪一个,即使两个都已启用。播放下面的代码,你就能感觉到发生了什么。

代码语言:javascript
复制
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")
}
} 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55342812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档