Any of your methods will work, but you will need to decide which method is best for your particular scenario, so the “right approach” is up to you. I had the same question about six months ago, and the following solution worked flawlessly for me:
SKSpriteNodes SKScene
, SKNode, SpriteKitButton, , SKSpriteNode UIButton SpriteKit, .
, , UIButton, SKScene, .
SKScenes , UIViewControllers UIViews SKScenes.
, , self.view?.presentScene... , , .
:
1) :
import SpriteKit
class SpriteKitButton: SKNode {
var button: SKSpriteNode!
var buttonUnpressedTexture: SKTexture!
var buttonPressedTexture: SKTexture!
var action: (String) -> Void
init(buttonUnpressedImage: String, buttonPressedImage: String, buttonAction: (String) -> Void) {
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
}
2) SKScene :
let artboardButton = SpriteKitButton(buttonUnpressedImage: "artboardButtonUnpressed.png", buttonAction: openArtboard)
artboardButton.position = CGPoint(x: ..., y: ...)
artboardButton.zPosition = 0.0
artboardButton.userInteractionEnabled = true
self.addChild(artboardButton)
func openArtboard() {
}
touches SKNode, , , , . , , .
for 20 .
willMoveFromView, .