如果选中了按钮,如何围绕按钮绘制圆圈?

时间:2017-11-13 15:28:46

标签: swift button sprite-kit

我的场景如下: enter image description here

我正在使用df.set_index('id')['token_list'].\ apply(pd.Series).stack().reset_index(name='V').\ groupby('V')['id'].apply(list).to_dict() Out[359]: {'a': [1, 3], 'b': [1], 'c': [1, 2, 4], 'd': [2], 'e': [3], 'f': [3, 4]} ,我用四个按钮创建了这个场景。

如果用户点击其中一个按钮,则所选按钮周围应显示黑色圆圈(并留在那里)。如果用户选择另一个按钮,则前一个圆圈将消失,新按钮将变为圆形。

有谁知道怎么做?

这是我的代码:

SpriteKit

1 个答案:

答案 0 :(得分:1)

有几种方法可以实现这一目标。我会做什么(你应该考虑在你有4个同一个对象的时候这样做)是为你的按钮创建子类。这样,您可以向子类添加一个属性,指示按钮isSelected是否相应更改。

class Button: SKSpriteNode {

    var isSelected = false {
        didSet {
            if isSelected {
                background.isHidden = false
            }
            else {
                background.isHidden = true
            }
        }
   }

    //you can design the init to better suit however you want to differentiate between your buttons
    init(color: String) {

        //create an image of a black circle that you want to use as your border that is slightly larger than your other images
        let texture = SKTexture(imageNamed: "blackCircle")

        super.init(texture: nil, color: .clear, size: texture.size())

        let background = SKSpriteNode(texture: texture)
        background.zPosition = 0
        addChild(background)

        //add your color image here based on passed in parameters
        //but make sure that the zPosition is higher than 0
        let buttonTexture = SKTexture(imageNamed: color)

        let button = SKSpriteNode(texture: buttonTexture)
        button.zPosition = 1
        addChild(button)
    }
}
在你的gameScene中我会将按钮存储在一个数组中

private var blueButton: Button!
private var purpleButton: Button!
private var redButton: Button!
private var orangeButton: Button!
private var buttons = [Button]()

//example of creating your new button
blueButton = Button(color: "blue")
blueButton.position = CGPoint(x: blah, y: blah)
blueButton.zPosition = 1
addChild(blueButton)
buttons.append(blueButton)

//previously selected Button so that you know which one to unselect
private var prevSelectedButton: Button!

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches{
        let locationUser = touch.location(in: self)

        //unhighlight the last selected button
        prevSelectedButton.isSelected = false

        if atPoint(locationUser) == blueButton {
            blueButton.isSelected = true
            prevSelectedButton = blueButton  
        }
        else if atPoint(locationUser) == purpleButton {
            purpleButton.isSelected = true
            prevSelectedButton = purpleButton  
        }
        else if atPoint(locationUser) == redButton {
            redButton.isSelected = true
            prevSelectedButton = redButton  
        }
        else if atPoint(locationUser) == orangeButton {
            orangeButton.isSelected = true
            prevSelectedButton = orangeButton 
        }
    }
}

值得注意的是我会处理Button类中的touches事件以真正封装功能,但这超出了这个问题的范围

此外,如果您使用的是shapeNodes而不是SpriteNodes,您仍然可以使用此方法,只需在彩色圆圈后面添加一个黑色圆圈