How to present a modal view in sprite kit?

白昼怎懂夜的黑 提交于 2019-12-03 15:33:42

Smick pointed me in the right direction for a similar problem I was having. I needed to send a message to the main ViewController from my SKScene. This did it for me:

In your SKScene, import the ViewController

#import "MyViewController.h"

Then send it a message:

[(MyViewController *)self.view.window.rootViewController  myMethod];

You could try creating a method in the ViewController that opens the GameKit view for you, that is triggered from the SKScene. Thanks Smick!

presentModalViewController must be called on the view controller your SKView sits on.

You can also set up the settings view in the storyboard, ie

Then if you add a sprite and use for a button i.e. _settingsBtn it will perform the segue

    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch* touch = [touches anyObject];
        CGPoint location = [touch locationInNode:self];

        if ([_settingsBtn containsPoint:location]) {
            UIViewController *vc = self.view.window.rootViewController;
            [vc performSegueWithIdentifier:@"settingsPushSegue" sender:self];
        }
    }

Then you can use a unwind segue to remove it, just a a UIButton.

Have this in your view controller class..

- (IBAction)unwindToHideSettingsModal:(UIStoryboardSegue *)unwindSegue
{
    //NSLog(@"UNWILD");
}

So now on the storyboard, control drag from your button you added to the green exit segue, and select the above unwind segue.

Now you will have it show and hide as expected. You can design your settings UI ect in the storyboard.

That should get you started.

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