Swift 我想改变按钮的形象,只要用户点击按钮?
只要用户点击按钮,我就想更改按钮的图像,但如果用户从屏幕上松开手指,我会将图像更改回原始图像(我使用的是Sprite工具包) 我的代码:Swift 我想改变按钮的形象,只要用户点击按钮?,swift,sprite-kit,imagebutton,Swift,Sprite Kit,Imagebutton,只要用户点击按钮,我就想更改按钮的图像,但如果用户从屏幕上松开手指,我会将图像更改回原始图像(我使用的是Sprite工具包) 我的代码: var SettingButton = SKSpriteNode(imageNamed: "SettingButton1.0") override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { for touch in touches{
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
}
}
}
var SettingButton=SKSpriteNode(图像名为:“SettingButton1.0”)
覆盖func touchesStart(touchs:Set,带有事件:UIEvent?{
接触{
让locationUser=touch.location(in:self)
如果atPoint(位置用户)=设置按钮{
让SettingButton=SKSpriteNode(ImageName:“SettingButton2.0”)//更改图像
}
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
用于触摸{
让locationUser=touch.location(in:self)
如果atPoint(位置用户)=设置按钮{
//将图像更改回原始图像
}
}
}
尝试交换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
}
}
}
var buttonTextureUp=SKTexture(图像名为:“SettingButton1.0”)
var buttonTextureDown=SKTexture(图像名为:“SettingButton2.0”)
var settingButton=SKSpriteNode(纹理:buttonTextureUp)
覆盖func touchesStart(touchs:Set,带有事件:UIEvent?{
接触{
让locationUser=touch.location(in:self)
如果atPoint(位置用户)=设置按钮{
settingButton.texture=buttonTextureDown//更改图像
}
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
用于触摸{
让locationUser=touch.location(in:self)
如果atPoint(位置用户)=设置按钮{
settingButton.texture=buttonTextureUp//将图像更改回原始图像
}
}
}
您的问题是什么?你的代码编译吗?你有运行时错误吗?你的代码没有达到你期望的效果?请更具体一点。我不知道如何在touchesBegind函数@Dávid Pásztor中更改上面按钮的图像。如果调用touchescanced,请不要忘记将纹理更改为buttonTexture up