Question

I have a view that I already created using a xib file.

Now I would like to add some small elements to this view that would make use some of the physics animations from SpriteKit, so now I need an SKView.

Is it possible to add an SKView as a Subview of the view that corresponds to my xib view? I tried this and it does not seem to show anything.

The following is in the ViewController corresponding to my XIB view:

this.myCustomSKView = new CustomSKView()
this.View.AddSubview( this.myCustomSKView );

and the ViewController for my custom SKView has:

    public override void ViewWillLayoutSubviews ()
    {
        base.ViewWillLayoutSubviews ();
        if(this.SKView.Scene == null)
        {
            this.SKView.ShowsFPS = true;
            this.SKView.ShowsNodeCount = true;
            this.SKView.ShowsDrawCount = true;

            var scene = new MyCustomSKScene (this.SKView.Bounds.Size);
            this.SKView.PresentScene (scene);
        }
    }
Was it helpful?

Solution 2

I don't know what exactly you are trying to achieve but I think instead of SpriteKit you may wanna check out the UIKitDynamics which provides "physics-related capabilities and animations to views and other dynamic items". I would suggest you look at the apple doc first https://developer.apple.com/library/ios/samplecode/DynamicsCatalog/Introduction/Intro.html then a really nice tutorial on raywenderlich.com http://www.raywenderlich.com/50197/uikit-dynamics-tutorial

Hope this helps...

OTHER TIPS

I just gave this a try, and worked.

  1. Started with single view app.
  2. Dragged in myScene.m / .h from another sprite kit app into my project.
  3. In storyboard dragged in a UIView, and set class to SKView in storyboard.
  4. Created an outlet from that to the VC class (in my case called it myGame)
  5. Added the #import in the VC class
  6. Also copied from the demo project viewDidLoad

This is the only change i made

-(void)viewDidLoad {
   [super viewDidLoad];
   // Next line is all I changed...
   SKView * skView = (SKView *)self.view;
   skView.showsFPS = YES;
   skView.showsNodeCount = YES;

   // Create and configure the scene.
   SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
   scene.scaleMode = SKSceneScaleModeAspectFill;

   // Present the scene.
   [skView presentScene:scene];
}

I added in some other UIKit to kind show how its a little SK Game in a view.

enter image description here

Not sure if this is the best way but I hope I answers your question.

I agree with lionserdar, and you should check out UIKit Dynamics instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top