Hey
I'm trying to use a UISlider as i used it in normal app.
How do i show UISlider on Layer, i tried to add it like any other object
[self addChild:myslider z:2];
but doesnt work... well i guess because its not cocos2d object.
any ideas?
A fast, easy to use, free, and community supported 2D game engine
Hey
I'm trying to use a UISlider as i used it in normal app.
How do i show UISlider on Layer, i tried to add it like any other object
[self addChild:myslider z:2];
but doesnt work... well i guess because its not cocos2d object.
any ideas?
Read about UIKitLayer.
You basically need to add the UIElement to your core view - the OpenGLView.
anyways you would implement it this way:
[[[Director sharedDirector] openGLView] addSubview:mySlider];
don't forget to remove it from the superview, like so:
[mySlider removeFromSuperview];
and also release it, as it's pure obj-C and you need to take care of memory:
[mySlider release];
Hope this helps, Natan Avra.
Follow me on twitter: http://twitter.com/natanavra
Hi Natan,
Thanks for this tip. I am using a UISwitch, so I wanted to post that this is a good way to use switches in Cocos2D for anyone looking for help.
switchMusicCtl = [[ UISwitch alloc ] initWithFrame: CGRectMake(100, 10, 0, 0) ];
switchMusicCtl.on = YES; //set to be ON at start
switchMusicCtl.tag = 1; // this is not necessary - only to find later
[switchMusicCtl addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
[[[Director sharedDirector] openGLView] addSubview:switchMusicCtl];
[switchMusicCtl release]; // don't forget to release memory
The only thing is that if the view is rotated in Cocos2D to landscape, then the switch will need to be rotated. That is done with this line:
switchMusicCtl.transform = CGAffineTransformMakeRotation(M_PI/2);
You will need to removeFromSuperview when you leave the scene as Natan mentions. I did this in my 'BACK' button code:
-(void)gotoInfo: (id)sender {
SettingsScene * gs = [InfoScene node];
[switchMusicCtl removeFromSuperview];
[[Director sharedDirector] replaceScene:[FadeTransition transitionWithDuration:0.2 scene: gs withColorRGB:0x6dbb5d]];
}
Your logic code goes in the selector switchAction (or whatever you called it):
- (void)switchAction:(id)sender
{
// Your logic when the switch it used
// NSLog(@"switchAction: value = %d", [sender isOn]);
}
That's it. I hope this helps someone out there with UISwitch, UISlider, or other UIKit control.
Apologies for resurrecting a year-old thread, but the above code was extremely helpful for me last year when using 0.8.1. For anyone coming across this using 0.99.5 or later and wondering why it won't work with autorotation, try this:
[[[CCDirector sharedDirector] openGLView] addSubview: mySwitch]; // use this
//[[[[CCDirector sharedDirector] openGLView] window] addSubview: mySwitch]; // instead of this
And remove the rotation transform line. You'll probably have to adjust the position, too.
I was scratching my head for over an hour before I just tried attaching the subview directly to the openGLView, rather than the window. Autorotation works like a charm now.
I hope this saves someone else some time. :)
@eshirt,
I know this is not a direct answer to your question but it's worth looking at CCUIViewWrapper posted here http://www.cocos2d-iphone.org/forum/topic/6889. This works for me.
@wasabi - thanks for that link! It looks really interesting and I'm already thinking of some ways it could be useful.
Though, my previous post was actually saying that with 0.99.5 and autorotation, adding UIKit elements to a CCLayer is easier than it used to be and it rotates perfectly in both Landscape directions.
I just wanted to update the thread since anyone who used the previous technique would have to modify a couple lines to get it to work.
You must log in to post.