how to create menu on any other View in cocos2D - cocos2d-iphone

how to create menu on any other View it mean's i m created a UIView and i want to add MenuButton on it. This is my code but it is not working properly.. The UIView is hides the MuneButton..
UIView *aview;
aview = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 300,250)];
aview.backgroundColor= [UIColor redColor];
[[[CCDirector sharedDirector] openGLView] addSubview:aview];
// Standard method to create a button
CCMenuItem *menuItem1 = [CCMenuItemImage
itemFromNormalImage:#"Icon.png" selectedImage:#"Icon.png"
target:self selector:#selector(NextButton:)];
menuItem1.position = ccp(100, 60);
CCMenu *starMenu = [CCMenu menuWithItems:menuItem1, nil];
starMenu.position = CGPointZero;
[self addChild:starMenu];
and
(void) NextButton: (CCMenuItem *) menuItem
{
NSLog(#"Button1");
}
how to add Menu on UIView. I Try with AddSubview removing addChild. And i try aview removing self..

Hope it help to you..
http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:index

When you add a subview to a UIView, you are essentially adding them on top of the view. The cocos layer is always at the bottom. You can add the view to the root view controller, but you'll take a performance hit.
You're going about this in the wrong way anyway. Put your button in a CCLayer subclass with a CCColorLayer background and add that to your running scene. In your case: self.

My Learn Cocos2D book (2nd Edition) has a chapter about UIKit integration that explains how you can place UIKit views in front of and behind the Cocos2D GL view while allowing all views (front views, cocos2d view, back views) to respond to touch events.

Related

How to create Side Menu on right side in Swift 3.0?

I am a beginner and I wanted to create a side menu on the right side.
So far I have a UIButton on right top side of viewController, what I want is when I click that button I want to show/hide slide menu with say 3 items…
when I click each item it will go to the different view controller. In my project, i am showing slide menu in only one viewController using AMSlideMenu. Thanks in advance.
Anuj just follow the steps-
Create a SideMenuViewController which is sub class of UIViewController , using storyboard how it will look according to the requirement.
Add this SideMenuViewController and its view as a child view controller in parent view controller by UIButton click.
When you done, remove SideMenuViewController from parent View controller and remove its view from parent view.
Repeat 2 and 3 for all view controllers.
Updated code :
Declare in your view controller -
var sideMenuViewController = SideMenuViewController()
var isMenuOpened:Bool = false
In viewDidLoad
sideMenuViewController = storyboard!.instantiateViewController(withIdentifier: "SideMenuViewController") as! SideMenuViewController
sideMenuViewController.view.frame = UIScreen.main.bounds
In your button Clicked event -
func openAndCloseMenu(){
if(isMenuOpened){
isMenuOpened = false
sideMenuViewController.willMove(toParentViewController: nil)
sideMenuViewController.view.removeFromSuperview()
sideMenuViewController.removeFromParentViewController()
}
else{
isMenuOpened = true
self.addChildViewController(sideMenuViewController)
self.view.addSubview(sideMenuViewController.view)
sideMenuViewController.didMove(toParentViewController: self)
}
}
For Animation:
let transition = CATransition()
let withDuration = 0.5
transition.duration = withDuration
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
sideMenuViewController.view.layer.add(transition, forKey: kCATransition)
There is no built-in control in iOS for side-menu. However, you could use different open source libraries to achieve your goal.
Have a look at the following libraries:
https://github.com/John-Lluch/SWRevealViewController
The detailed tutorial for this library:
http://www.appcoda.com/ios-programming-sidebar-navigation-menu/
http://www.raywenderlich.com/32054/how-to-create-a-slide-out-navigation-like-facebook-and-path

How to set initial focus to a button in a viewController from a UITabBarController?

I have a basic application with a UITabBarController which contains 2 UIViewControllers.
The first ViewController has a UIButton in it.
I'm trying to get the tabBar menu hidden and the button focused when the app launches.
I set the tabBar as hidden, and it does not show on launch as expected. Unfortunately, the button doesn't have the focus.
From what I understand from Apple doc, the "focus chain" engine will set focus on the first visible and focusable item in the window hierarchy.
Can anyone help me on this ?
Thank you.
Solution 1.
I think what you can do :
1.Subclass or Customize TabBarController and set
set first launch = true in viewdidload of tabController
> override weak var preferredFocusedView: UIView? { if
> (self.firstLaunch) {
> self.firstLaunch = false;
> return self.selectedViewController!.preferredFocusedView; } else {
> let view = super.preferredFocusedView
> return view; }
Hope it helps.It worked for me though
Your focus engine takes the rootView controller as TabBarController and then asks for preferred focus view which is returned as UITabBarItem we need to unfocus that subclassing the class and returning canBecomeFocus to NO.
if you want to change the focus of the element in the firtsViewController then you can overrirde func preferredFocusView and reutrn the view you want to be focussed else preferredFocusView
Solution 2. Just set the root View controller as the HomeViewController without the TabBarController and embed the TabBarController from the second page. This is because anyways you don't need to use TabBar on the first page why need to take care of its focus.
TabBar is set to hidden if you hide from the AppDelegate or the view which is loaded but it has the focus.

ViewDidLoad Not Called in UITabBar application

I am using Tabbar view controller as parent view controller. I am adding it from interface builder as shown in the below screen shot. Everything is working fine but the viewdidload,viewwillappear and viewdidappear methods are not calling from the begining. even if the viewcontroller is loading for the first time also. can any one please help me out code in my app delegate is
ScreenShot of IB: http://i58.tinypic.com/u4o6w.png
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.backgroundColor = [UIColor whiteColor];
self.tabBarController.tabBar.barTintColor = [UIColor blackColor];
self.window.rootViewController = self.tabBarController;
//[self.window addSubview:self.tabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
I am able to see the loaded views but the methods are not calling.
Can any one please help me out

Restarting cocos2d's scene from UIKit Menu

i have a UIKit menu based on UIButtons, when i click on one of them i'm firing a cocos2D scene. i'm setting up the CCDirector in the AppDelegate :
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector *director = [CCDirector sharedDirector];
[director setAnimationInterval:1.0/60];
[director setDisplayFPS:YES];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
and in the rootViewController (the viewController that will holds my EAGLView) i'm doing this in the viewDidLoad method :
glView = [EAGLView viewWithFrame:self.view.bounds
pixelFormat:kEAGLColorFormatRGBA8
depthFormat:0
];
glView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view insertSubview:glView atIndex:0];
[[CCDirector sharedDirector] setOpenGLView:glView];
CCScene *scene = [GameScene sceneWithParent:self];
[[CCDirector sharedDirector] runWithScene:scene];
i'm setting up my menu in the MainMenuViewController, from where i push the RootViewController in the stack of a navigationController. This works perfectly and my game starts without problems. I can also go back with a UIButton from the RootViewController (which holds the cocos2d scene) : - (IBAction)goBack {
[self.navigationController popViewControllerAnimated:YES];
[[CCDirector sharedDirector] end];}
but when i try to reload my game scene the application quit. i've tried this method :
restart CCDirector but in vain!! i've tried to pause the CCDirector and restart animation but also in vain, the error that i'm getting is in relation with the draw method of cocos2d!!
please help
i've just solved my problem. so if you are trying to use a UINavigationController you don't have to use [[CCDirector sharedDirector] end], because when the rootController is poped from the stack, all the scenes of cocos2d world will be released. But if you are trying to removing only the subview (not the hole controller) you have to end the CCDirector!! my problem was in a singleton class that i added as a child of GameScene, when i ended the CCDirector it still exists in the memory so i had to release it before recreating the game scene..hope this was clear.

Pass UIEvent from UIView in CCUIViewWrapper to CCLayer

I have a CCLayer with a UIViewController.view added to the layer via CCUIViewWrapper.
UIViewController *vc = [[UIViewController alloc] initWithNibName:#"Book" bundle:nil];
vc.view.backgroundColor = [UIColor whiteColor];
wrapper = [CCUIViewWrapper wrapperForUIView:vc.view];
wrapper.contentSize = CGSizeMake(320, 480);
[self addChild:wrapper];
The view has a UIButton, which ideally responds to Touch event, and call an IBAction.
But I can't figure out how to make the UIButton call the IBAction in CCLayer. The reason that the button calls an IBAction in CCLayer is that the action planned to do is to interact with the elements of CCLayer.
Can anyone give me some lights?