只要用户点击按钮,我就想更改按钮的图像?

I want to change the image of the button as long as user taps on the button?

我想在用户点击按钮时更改按钮的图像,但如果他从屏幕上松开手指,则将图像更改回原始图像(我使用的是 Sprite-Kit)

我的代码:

var SettingButton = SKSpriteNode(imageNamed: "SettingButton1.0")

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

        if atPoint(locationUser) == SettingButton{
            let SettingButton = SKSpriteNode(imageNamed: "SettingButton2.0") //change the image
        }
    }
}

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

        if atPoint(locationUser) == SettingButton{
            //change image back to original
        }
    }
}

尝试交换 SpriteNode 的纹理

var buttonTextureUp = SKTexture(imageNamed: "SettingButton1.0")
var buttonTextureDown = SKTexture(imageNamed: "SettingButton2.0")

var settingButton = SKSpriteNode(texture: buttonTextureUp)

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

        if atPoint(locationUser) == settingButton {
            settingButton.texture = buttonTextureDown //change the image
        }
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

    for touch in touches{ 
        let locationUser = touch.location(in: self)

        if atPoint(locationUser) == settingButton{
            settingButton.texture = buttonTextureUp //change image back to original
        }
    }
}