【问题标题】:How to change button images from another class?如何更改另一个类的按钮图像?
【发布时间】:2017-10-31 10:07:35
【问题描述】:

我是编码新手,我想知道如何从设置类更改菜单类中的按钮图像。

这里是菜单类:

class Mainmenu: SKScene{

 var ratingButton = SKSpriteNode(imageNamed: "RatingButton1")
}

在我的设置类中,我想通过单击按钮将此图像更改为“RatingButton2”。

这里是设置类:

class Settings: SKScene {

override func didMove(to view: SKView) {

 self.backgroundColor = SKColor.white

 let DCButton = SKSpriteNode(imageNamed: "ChangeButton")
 DCButton.position = CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.8)
 DCButton.setScale(0.53)
 self.addChild(DCButton)
 }

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

    for touch in touches{

        let locationUser = touch.location(in: self)

        if atPoint(locationUser) == DCButton {

        //Change the button image here

        }
      }
    }
  }

【问题讨论】:

  • 文档怎么了? (我认为你的代码在我看来可能更容易,因为垂直空间的浪费更少。)
  • @greybeard 完成

标签: swift button sprite-kit imagebutton


【解决方案1】:

Swift 3+ 使用通知中心更新您的按钮图像。

// 定义标识符

let notificationName = Notification.Name("NotificationIdentifier")

// Register to receive notification in Mainmenu
NotificationCenter.default.addObserver(self, selector: #selector(receivedNotification), name: notificationName, object: nil)


// Post notification into Settings write this line into "touchesBegan" method
NotificationCenter.default.post(name: notificationName, object: nil)


// Stop listening notification if required.
NotificationCenter.default.removeObserver(self, name: notificationName, object: nil)

通知处理程序

func receivedNotification(notification: Notification){
   // Set your button image here...
}

【讨论】:

  • 我不知道在 func @Shiv Kumar 中具体放什么
  • 将此行写入您的 Menucontroller viewdidload。 // 在 Mainmenu 中注册接收通知 NotificationCenter.default.addObserver(self, selector: #selector(receivedNotification), name: notificationName, object: nil) func receivedNotification(notification: Notification){ // 在此处设置按钮图像... }
  • 将此行写入您的“touchesBegan” // 将通知发布到设置中将此行写入“touchesBegan”方法 NotificationCenter.default.post(name: notificationName, object: nil)
  • 在菜单类中,我两次收到此错误:Use of unresolved identifier 'receivedNotification' @Shiv Kumar
【解决方案2】:

我不建议使用 NSNotificationCenter 来完成这项任务,它会为很少使用的东西增加很多开销。

现在很遗憾,我不知道您的布局是如何构成的,所以我不确定哪种答案最适合您。

如果菜单和设置同时在场景中,那么您可以搜索场景找到您要查找的按钮:

class Mainmenu: SKScene{

    var ratingButton = SKSpriteNode(imageNamed: "RatingButton1")
    ratingButton.name = "ratingButton"

}

class Settings: SKScene {

    override func didMove(to view: SKView) {

        self.backgroundColor = SKColor.white

        let DCButton = SKSpriteNode(imageNamed: "ChangeButton")
        DCButton.position = CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.8)
        DCButton.setScale(0.53)
        self.addChild(DCButton)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches{
            let locationUser = touch.location(in: self)
            if atPoint(locationUser) == DCButton {
                let button = scene.childNode(withName:"//ratingButton") as! SKSpriteNode
                button.texture = SKTexture(imageNamed:"RatingButton2")
            }
        }
    }
}

如果它不在现场并且您正在展示它,那么您想使用userData 预先指定您的按钮是什么

let menu = Mainmenu(...)
menu.userData = ["ratingButton":SKTexture(imageNamed:textureName)] //texture name is either Ratingbutton1 or RatingButton2 depending on state
self.view.presentScene(menu)

然后在viewDidLoad,只需阅读userData

func viewDidLoad(...)
{
    ratingButton.texture = userData?["ratingButton"]

}

【讨论】:

  • 这两部分的代码我必须放在哪些场景中? @KnightofDragon
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
相关资源
最近更新 更多