crashoverride777
crashoverride777

Reputation: 10664

3D Touch and SpriteKit

Has anyone played around with 3D touch and spritekit?.

I have been watching some tutorials about 3d touch and it seems fairly simple for the most part. However all tutorials are about regular apps using multiple view controllers (using storyboard IDs, previewControllers etc)

Is it possible to integrate 3D touch with SpriteKit which usually only has 1 ViewController.

My plan would be to

1) HomeScreen shortcuts which would load different scenes (shop scene, level1, level 2 etc)

2) Peek and Pop, maybe for menus. Can you even use peek and pop for things such as SKNodes?

Thanks for any tips or suggestions

Upvotes: 4

Views: 677

Answers (1)

Andy Merhaut
Andy Merhaut

Reputation: 305

Yes, you can integrate 3D Touch and SpriteKit.

Using the default SpriteKit game project template (Swift), set your GameScene to be a UIViewControllerPreviewingDelegate and implement the protocol's methods, like this:

public class GameScene: SKScene, UIViewControllerPreviewingDelegate {
  public func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {  
    return UIViewController()
  }

  //Pop
  public func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
    return
  }
}

In GameViewController.swift's viewDidLoad, add the following:

if let scene = GameScene(fileNamed:"GameScene") {
  if self.traitCollection.forceTouchCapability == .Available {
    registerForPreviewingWithDelegate(scene, sourceView: scene.view!)
  }
}

In the delegate method, I am returning an empty view controller. You can create a class-level variable and instantiate this VC with appropriate peek-type views. Again, a very simple example. If you want to add 3D Touch from an AppIcon interaction, you will need to modify Info.plist and add capabilities from the AppDelegate. There is a good video on this topic. Good luck!

Upvotes: 1

Related Questions