Search code examples
iosswiftsprite-kit3dtouch

Peek and Pop Function SpriteKit


I have a menu in an `SKScene'. I'm trying to integrate a Peek and Pop function into it. I have multiple possible selections so my problem is how to identify which one is being force touched.

This is my code:

import SpriteKit
import UIKit

class Menu: SKScene, UIViewControllerPreviewingDelegate {

//SETUP Menu


 func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        return UIViewController()
    }

    //POP
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
        return
    }

}

GameViewController.swift:

import UIKit
import SpriteKit

class GameViewController: UIViewController {


    override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = GameScene(fileNamed:"GameScene") {
       if self.traitCollection.forceTouchCapability == .available {
          registerForPreviewing(with: scene as! UIViewControllerPreviewingDelegate, sourceView: scene.view!)
          }
         }

       // Move to Menu

 }
}

I'm pretty sure that I should be passing the information in the sourceView field. But since this is in GameViewController I don't know what to do. And I don't know how to do this inside the Menu, where would I put this code?

 if let scene = GameScene(fileNamed:"GameScene") {
            if self.traitCollection.forceTouchCapability == .available {
                registerForPreviewing(with: scene as! UIViewControllerPreviewingDelegate, sourceView: ButtonPressed)
            }
        }

Solution

  • You will not be able to use Peek and Pop in a SKScene or a SKSpriteNode menu like you are trying, I was trying to do the same in one of my games.

    Peek and Pop only works with UIKit, specifically it allows you to preview (Peek) and than open (Pop) UIViewControllers.

    In SpriteKit you normally only have 1 ViewController, GameViewController. That GameViewController should only handle your SKScene presentation, your actual UI is/should be done using SpriteKit APIs such as SKLabelNodes, SKSpriteNodes etc directly in the relevant SKScenes. Therefore you dont really use UIKit and have not much you can preview with Peek and Pop.

    There are some exceptions to this so it depends what kind of game you are making. For example in one of my games I am using a UICollectionViewController for my level select menu (40 levels) as they are perfect for that scenario (cell reuse, smooth scrolling etc). Because that menu is made with UIKit I can register for peek and pop when you force touch on a cell. I than have another UIViewController with some UILabels, UIImageViews etc I specifically use for the Peeking (see picture) and when I pop I just start the actual level.

    So in summary there is not many situations where you can use Peek and Pop in SpriteKit. I also would not recommend to try to design your UI with UIKIt just so that you can use Peek and Pop. Hopefully Apple will update the API one day so when can peek and pop SKNodes or SKScenes, that would be awesome.

    Hope this helps

    enter image description here