This is apparently a fairly common problem that people encounter with SpriteKit games, so it helps to understand the differences between SpriteKit games and UIKit applications.
When you make a regular UIKit application, for example. YouTube, Facebook, you will use ViewControllers, CollectionViews, Views, etc. For each screen / menu that you see (main screen, channel screen, subscription channel screen, etc.). Therefore, you should use the UIKit API for such as UIButtons, UIImageViews, UILabels, UIViews, UICollectionViews, etc. For this, visually we will use storyboards.
SpriteKit, , -. SKScenes , (MenuScene, SettingsScene, GameScene, GameOverScene ..) 1 ViewController (GameViewController). GameViewController, SKView, SKScenes.
, SKScenes, API- SpriteKit, SKLabelNodes, SKSpriteNodes, SKNodes .. , SpriteKit, .
, , SKScene, , GameViewController, - SKScenes. GameViewController , . 1 (GameScene → GameOverScene).
GameViewController , , SKScenes, GameViewController, SKScenes. / , .
SpriteKit, -
class GameScene: SKScene {
lazy var scoreLabel: SKLabelNode = {
let label = SKLabelNode(fontNamed: "HelveticaNeue")
label.text = "SomeText"
label.fontSize = 22
label.fontColor = .yellow
label.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
return label
}()
override func didMove(to view: SKView) {
addChild(scoreLabel)
}
}
, , , SKSpriteNode , touchhesBegan touchEnded SKAction .
enum ButtonName: String {
case play
case share
}
class GameScene: SKScene {
lazy var shareButton: SKSpriteNode = {
let button = SKSpriteNode(imageNamed: "ShareButton")
button.name = ButtonName.share.rawValue
button.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
return button
}()
override func didMove(to view: SKView) {
addChild(shareButton)
}
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let node = atPoint(location)
if let nodeName = node.name {
switch nodeName {
case ButtonName.play.rawValue:
case ButtonName.share.rawValue:
let action1 = SKAction.scale(to: 0.9, duration: 0.2)
let action2 = SKAction.scale(to: 1, duration: 0.2)
let action3 = SKAction.run { [weak self] in
self?.openShareMenu(value: "\(self!.score)", image: nil)
}
let sequence = SKAction.sequence([action1, action2, action3])
node.run(sequence)
default:
break
}
}
}
}
}
, ,
https://nathandemick.com/2014/09/buttons-sprite-kit-using-swift/
DemoBots Apple .
, , .. , .
UIActivityController API, . 1 , . , , SKScene, .
func openShareMenu(value: String, image: UIImage?) {
guard let view = view else { return }
var activityItems = [AnyObject]()
let text = "Can you beat my score " + value
activityItems.append(text as AnyObject)
if let image = image {
activityItems.append(image)
}
let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
if Device.isPad {
activityController.popoverPresentationController?.sourceView = view
activityController.popoverPresentationController?.sourceRect = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
activityController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.init(rawValue: 0)
}
activityController.excludedActivityTypes = [
UIActivityType.airDrop,
UIActivityType.print,
UIActivityType.assignToContact,
UIActivityType.addToReadingList,
]
view.window?.rootViewController?.present(activityController, animated: true)
}
, (. )
openShareMenu(value: "\(self.score)", image: SOMEUIIMAGE)
,