Integrating a SpriteKit view into a xib view

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 17:08:36

问题


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);
        }
    }

回答1:


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...




回答2:


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.

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.



来源:https://stackoverflow.com/questions/19258567/integrating-a-spritekit-view-into-a-xib-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!