Integrating a SpriteKit view into a xib view - ios7

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

I just gave this a try, and worked.
Started with single view app.
Dragged in myScene.m / .h from another sprite kit app into my project.
In storyboard dragged in a UIView, and set class to SKView in storyboard.
Created an outlet from that to the VC class (in my case called it myGame)
Added the #import in the VC class
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.

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

Related

How do I load a .sks linked with .swift class into a UIViewController in my Objective-C project

I have an Objective-C based project in Xcode. I'm trying to add a SpriteKit scene as a smaller view in a view controller. The SpriteKit files are in swift though. I've added Floor1.sks and linked it with Floor1.swift.
Now I'm trying to load it into my Objective-C ViewController.m file. In my storyboard I made the view to the class SKView, and I think I properly made a bridging-header file. My code to insert it at the moment is:
GKScene *scene = [GKScene sceneWithFileNamed:#"Floor1"];
Floor1 *sceneNode = (Floor1 *)scene.rootNode;
sceneNode.scaleMode = SKSceneScaleModeAspectFit;
SKView *skView = (SKView *)_skView;
[skView presentScene:sceneNode];
skView.showsFPS = YES;
skView.showsNodeCount = YES;
//skView loads blank grey screen
The view loads when I run the app successfully, but it's just an empty view with a light grey background with the node count which is 0, and the FPS. What am I doing wrong, how come it won't load my proper view?

Spritekit iAds messing with scene size

I am making a game using spritekit and everything works perfectly except when I add the code to add an iAd. When I add the iAd everything works but it seems that the iAd is resizing the scene when it is being displayed.
This is my code to display the iAd, it is in the ViewController.m :
#import <iAd/iAd.h>
#import "ViewController.h"
#import "MainMenu.h"
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.originalContentView;
skView.showsFPS = YES;
// Create and configure the scene.
SKScene * scene = [MainMenu sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
self.canDisplayBannerAds = YES;
// Present the scene.
[skView presentScene:scene];
}
Like I say, the iAd shows up, and all the function properly but the when the iAd is displayed it makes the scene smaller, is there a method or something that allows the scenes to not be resized?
An help is much appreciated, thanks!
Ok so I sort of figured it out, I was using scaleMode of SKSceneScaleModeAspectFit I have now changed it to SKSceneScaleModeFill this seems to make the scene shorter but it is not as noticeable as the aspectFit scale mode. I have also noticed that the iAd does not act like a sprite as it actually distorts or resizes the screen. If anyone knows how to create the iAd on top of the view, like sprite, please add a comment or solution.
EDIT
I have just figured out that one can add an ADBannerView to the viewcontroller in the interface builder, this will show the ad on all scenes. I am now trying to figure out how this can be set to not display on specific scenes seeing spritekit only uses one viewcontroller.
If you add the ad by adding in the AdBannerView then you have to create seperate methods in the view controller to turn the ads on or off, by creating these seperate methods it allows one to have manual control over the ads in the view controller from any scene.
In the view controller you have a scene that you create, this scene variable/object has a property of tag or userdata. So if your game goes to a different scene you can call
[super scene] setTag:x]
Every time that the NSTimer calls my control method it checks the value of the scenes tag in the view controller and based on that value it will remove or re-display the ad.
Just a thought, but you could also put the AdBannerView in via the Storyboard editor, and then set
self.canDisplayBannerAds = NO;
That way it will just overlay the SKScene instead of shrinking it.
Hope this helps!
There is another way to fix this problem. You might want to have a look at this Thread.
iAd in Spritekit
Just a sneak preview:
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.delegate = self;
[adView setFrame:CGRectMake(0, 0, 1024, 768)]; // set to your screen dimensions
[adView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:adView];

How can I enable iAd when using Sprite Kit?

In IOS 7, we can enable iAd very easy when using:
self.canDisplayBannerAds = YES;
in code ViewDidLoad of UIViewController
However, I cannot use this in my ViewController (the ViewControl that load SKScene). My game is crash when loading.
So how can I active iAd in my game (using Sprite Kit)?
You can only show iAd in a UIViewController subclass. You are doing it right. Put the following code in your UIViewController and not in the SKScene.
Please refer to this tutorial for the exact code: http://tutorials.veasoftware.com/2013/10/10/how-to-add-iads/
It's possible that you are accessing the view property. When using iAds you need to replace .view with .originalContentView. For example:
SKView* skView = (SKView *)self.view;
is replaced with
SKView* skView = (SKView *)self.originalContentView;

Custom UIView Not Showing Accessibility on Voice Over

I'm trying to get voice over working with an openGL view, specifically from the cocos2d framework.
From the Apple Accessibility guide I followed this section: Make the Contents of Custom Container Views Accessible
I've subclassed the view (CCGLView for cocos2d people), which is a UIView, to implement the informal UIAccessibilityContainer protocol.
UIAccessibilityContainer implementation in my subclassed UIView:
-(NSArray *)accessibilityElements{
return [self.delegate accessibleElements];
}
-(BOOL)isAccessibilityElement{
return NO;
}
-(NSInteger)accessibilityElementCount{
return [self accessibilityElements].count;
}
-(NSInteger)indexOfAccessibilityElement:(id)element{
return [[self accessibilityElements] indexOfObject:element];
}
-(id)accessibilityElementAtIndex:(NSInteger)index{
return [[self accessibilityElements] objectAtIndex:index];
}
This code is getting called and -(NSArray *)acessibilityElements is returning an array of UIAccessibilityElements. However the voice over controls are not showing up when I touch the screen. Any ideas on what I'm missing or doing wrong?
Other Information:
I'm using a storyboard and adding the CCGLView to the UIView in the storyboard. The _director.view is the CCGLView that I subclassed.
// Add the director as a child view controller.
[self addChildViewController:_director];
// Add the director's OpenGL view, and send it to the back of the view hierarchy so we can place UIKit elements on top of it.
[self.view addSubview:_director.view];
[self.view sendSubviewToBack:_director.view];
For a while I suspected that because I added the subview that this was causing it not to show up, but I also tried subclassing the UIView in the storyboard the same way but it was also not working.
Also this is how I am creating each UIAccessibilityElement in the array.
UIAccessibilityElement *elm = [[UIAccessibilityElement alloc] initWithAccessibilityContainer:view];
elm.accessibilityFrame = f;
elm.accessibilityLabel = t.letter;
elm.isAccessibilityElement = YES;
elm.accessibilityHint = #"Button";
elm.accessibilityValue = t.letter;
elm.accessibilityTraits = UIAccessibilityTraitButton;
Found a solution that is working now, in case anyone has this problem. -(id)accessibilityElementAtIndex:(NSInteger)index was returning a properUIAccessibilityElement but it looks like it wasn't getting retained by whatever Accessibility API is using it. I made a strong NSArray property to hold the UIAccessibilityElements and now it is working fine.

JTRevealSidebar initialized from tableview

I would like to implement the JTRevealSidebar project (a sidebar feature very similar to the sidebar in the facebook app) into my project. My only problem is that my main view from where i want to initialize the sidebar is a UITableView, and not a UIView, which the JTRevealSidebar is setup for. Has anybody tried setting this up before with a tableview or know if it's possible?
You can return any subclass of UIViews in your sidebarDelegate callback
- (UIView *)viewForLeftSidebar {
// Use applicationViewFrame to get the correctly calculated view's frame
// for use as a reference to our sidebar's view
CGRect viewFrame = self.navigationController.applicationViewFrame;
UIView *anyView = self.leftView;
if ( ! anyView) {
anyView = [[AnySubclassOfUIView alloc] initWithFrame:viewFrame];
[self.view addSubview:anyView];
}
return anyView;
}