How would I implement a splash screen that animates away? - objective-c

This is what I have so far:
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
-(void)animate
{
[UIView animateWithDuration:1 animations:^{
splashImage.frame = CGRectMake(0,480,320,480);
}];
[UIView commitAnimations];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
splashImage = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:#"carolinaWolf.png"]];
splashImage.frame = CGRectMake(0, 0, 320, 480);
[self.tabBarController.view addSubview:splashImage];
[self animate];
return YES;
}
My initial view in storyboard is a TabBarController. What I want to happen is: after I finish loading new content into the application, I want to splash screen to animate down and off the screen. This worked before I started using storyboard, why won't it work now?
My AppDelegate.h looks like this:
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UITabBarController *tabBarController;
#end
UIImageView *splashImage;

You can directly just add your splashImage to self.window.rootViewController like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIImage *splashImage = [UIImage autoAdjustImageNamed:#"Default.png"];
UIImageView *splashImageView = [[UIImageView alloc] initWithImage:splashImage];
[self.window.rootViewController.view addSubview:splashImageView];
[self.window.rootViewController.view bringSubviewToFront:splashImageView];
[UIView animateWithDuration:1.5f
delay:2.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
splashImageView.alpha = .0f;
CGFloat x = -60.0f;
CGFloat y = -120.0f;
splashImageView.frame = CGRectMake(x,
y,
splashImageView.frame.size.width-2*x,
splashImageView.frame.size.height-2*y);
} completion:^(BOOL finished){
if (finished) {
[splashImageView removeFromSuperview];
}
}];
return YES;
}

This is how I fade away my splash screens:
#pragma mark Fading Default.png Animation
#interface FadeDefault : UIViewController
#end
#implementation FadeDefault
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *fader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default#2x.png"]];
[fader setFrame:CGRectMake(0,0,320,480)];
/*if ( IDIOM == IPAD ) /* Optional */
{
fader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default-Portrait~ipad.png"]];
[fader setFrame:CGRectMake(0,0,768,1024)];
}else
{
fader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default#2x.png"]];
[fader setFrame:CGRectMake(0,0,320,480)];
}*/
self.view = fader;
[fader release];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(imageDidFadeOut:finished:context:)];
self.view.alpha = 0.0;
[UIView commitAnimations];
}
- (void)imageDidFadeOut:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
[self.view removeFromSuperview];
}
#end
#pragma mark - Application lifecycle
Keep in mind this is an example, although code I actually use and I know works, you need to keep your other didFinishLaunchingWithOptions code in place. Just add the fader code to you existing code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/* custom fading Default.png animation */
FadeDefault * fader = [[FadeDefault alloc] init];
[window addSubview:navigationController.view];
[window addSubview:fader.view];
[fader release];
[window makeKeyAndVisible];
return YES;
}

Related

AdMob integration destorts the Hello World Label text

I have created a simple hello world project for cocos2d-x 3.2. Added AdMob banner. The essence of adding banner is to create a view, then add into that view first the cocos2d-x content and then banner content:
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self addChildViewController:_contentController];
[contentView addSubview:_contentController.view];
[_contentController didMoveToParentViewController:self];
[contentView addSubview:_bannerView];
self.view = contentView;
The result is that on iPhone, iPhone Retina, iPad Retina (but not in iPad) the Hello World text is distorted like this:
At the same time that stats text is not distorted:
I cannot understand what happens and why. Here is my full code:
.hfile
#import <UIKit/UIKit.h>
#interface BannerViewController : UIViewController
- (instancetype)initWithContentViewController:(UIViewController *)contentController;
- (void) hideBanner;
- (void) showBanner;
#end
and hers is .mm file
#import "BannerViewController.h"
#import "GADBannerView.h"
#interface BannerViewController () <GADBannerViewDelegate>
#end
#implementation BannerViewController {
GADBannerView *_bannerView;
UIViewController *_contentController;
Boolean _bannerLoaded;
}
- (instancetype)initWithContentViewController:(UIViewController *)contentController
{
self = [super init];
if (self != nil) {
// use kGADAdSizeBanner for a small banner in iPad
_bannerView = [[GADBannerView alloc] initWithAdSize: kGADAdSizeSmartBannerPortrait]; // scaled banner dependent on device size
_bannerView.adUnitID = #"ca-app-pub-874958723945898/8247587858";
_bannerView.delegate = self;
_contentController = contentController;
_bannerLoaded = NO;
}
return self;
}
- (void)loadView
{
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self addChildViewController:_contentController];
[contentView addSubview:_contentController.view];
[_contentController didMoveToParentViewController:self];
[contentView addSubview:_bannerView];
self.view = contentView;
}
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return [_contentController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
#endif
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [_contentController preferredInterfaceOrientationForPresentation];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [_contentController supportedInterfaceOrientations];
}
// For animation
- (void)viewDidLayoutSubviews
{
CGRect contentFrame = self.view.bounds;
CGRect bannerFrame = CGRectZero;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
bannerFrame = _bannerView.frame;
#else
bannerFrame.size = [_bannerView sizeThatFits:contentFrame.size];
#endif
bannerFrame.origin.x = (contentFrame.size.width - bannerFrame.size.width) / 2;
if (_bannerLoaded) {
//contentFrame.size.height -= bannerFrame.size.height;
bannerFrame.origin.y = contentFrame.size.height - bannerFrame.size.height;
} else {
bannerFrame.origin.y = contentFrame.size.height;
}
_contentController.view.frame = contentFrame;
_bannerView.frame = bannerFrame;
}
- (void)viewDidLoad {
[super viewDidLoad];
_bannerView.rootViewController = self;
[self.view addSubview:_bannerView];
GADRequest *request = [GADRequest request];
[_bannerView loadRequest:request];
}
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView
{
NSLog(#"adViewDidReceiveAd");
_bannerLoaded = YES;
[UIView animateWithDuration:0.25 animations:^{
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}];
}
- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error
{
NSLog(#"adView didFailToReceiveAdWithError");
_bannerLoaded = NO;
[UIView animateWithDuration:0.25 animations:^{
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}];
}
- (void) hideBanner{
//TODO:
}
- (void) showBanner{
//TODO:
}
- (void)dealloc {
_bannerView.delegate = nil;
[_bannerView release];
[super dealloc];
}
#end
// </GADBannerViewDelegate>
And I use it in AppController.mm like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
// Init the CCEAGLView
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH24_STENCIL8_OES
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0];
// Use RootViewController manage CCEAGLView
_viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
_viewController.wantsFullScreenLayout = YES;
_viewController.view = eaglView;
_bannerViewController = [[BannerViewController alloc] initWithContentViewController:_viewController];
// Set RootViewController to window
if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
{
// warning: addSubView doesn't work on iOS6
[window addSubview: _bannerViewController.view];
//[window addSubview: _viewController.view];
}
else
{
// use this method on ios6
[window setRootViewController:_bannerViewController];
// [window setRootViewController:_viewController];
}
[window makeKeyAndVisible];
[[UIApplication sharedApplication] setStatusBarHidden:true];
// IMPORTANT: Setting the GLView should be done after creating the RootViewController
cocos2d::GLView *glview = cocos2d::GLView::createWithEAGLView(eaglView);
cocos2d::Director::getInstance()->setOpenGLView(glview);
cocos2d::Application::getInstance()->run();
return YES;
}
What is wrong here?
I have found the problem. I appears that there is a time in hours written with black color like in the image, which on on the Hello World label and hence it distorts:
The time is shown is simulators status bar. In order to hide it I added this:
//fix not hide status on ios7
- (BOOL)prefersStatusBarHidden
{
return YES;
}
into BannerViewController class and that's it!

How to make MPMoviePlayerController fullscreen in landscape and not fullscreen in portrait

I have a MPMoviePlayerController that is initialized as follows:
//Code in my UIViewController
#property (nonatomic, strong) UIView *myVideoView;
#property (nonatomic, strong) MPMoviePlayerController *myVideoPlayer;
- (void) initializeVideoPlayer
{
CGRect frame = CGRectMake(0, 70, self.view.frame.size.width, 200);
self.myVideoView = [[UIView alloc] initWithFrame:frame];
[self.view addSubview:self.myVideoView];
NSURL *videoURL = [NSURL fileURLWithPath:path];
self.myVideoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
self.myVideoPlayer.controlStyle = MPMovieControlStyleEmbedded;
self.myVideoPlayer.shouldAutoplay = YES;
[self.myVideoPlayer.view setFrame: self.myVideoView.bounds];
[self.myVideoView addSubview: self.myVideoPlayer.view];
//Play video
[self.myVideoPlayer prepareToPlay];
[self.myVideoPlayer play];
}
My question is how do I get the video to play in fullscreen when the user rotates the phone to landscape and not fullscreen when the phone is in portrait.
I've tried adding the following to my UIViewController
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
{
[self.myVideoPlayer setFullscreen:YES animated:YES];
}
else
{
[self.myVideoPlayer setFullscreen:NO animated:YES];
}
}
However, the problem with this is that once the player is in fullscreen, the willAnimateRotationToInterfaceOrientation no longer gets called; therefore, even when the user rotates back to portrait, the video is still in fullscreen.
Try using an MPMoviePlayerViewController instead of an MPMoviePlayerController. Just initialize it in your UIViewController and use its moviePlayer property as you would with a plain MPMoviePlayerController. If you subclass MPMoviePlayerViewController you can control what's happening when the device rotates by implementing willAnimateRotationToInterfaceOrientation etc.
In AppDelegate.h:
#property(nonatomic)BOOL allowRotation;
in AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
RootViewController * root = [[RootViewController alloc] init];
self.window.rootViewController = root;
//add two Notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(MPVisionVideoNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(MPVisionVideoNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void) moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification {
self.allowRotation = YES;
}
- (void) moviePlayerWillExitFullscreenNotification:(NSNotification*)notification {
self.allowRotation = NO;
}
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.allowRotation) {
return UIInterfaceOrientationMaskLandscapeRight ;
}
return UIInterfaceOrientationMaskPortrait;
}
//this can rotate the windows when to fullscreen state

UINavigationBar is not rotating

I am experiencing a little problem. I'm trying to develop an app with UINavigationController bar but it is not rotating although my tableView is.
Just a little introduction so you know what my application is right now : I have a UITableView which is created in IB and linked in my class ViewController.h and a UINavigationController created in the appDelegate.h. All the items in the tableView can be clicked and it will slide to another viewController but this part is not important for now. I'm just trying to support rotation in the main view for now.
I create the UINavigationController in my AppDelegate.h like this:
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navigation;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UIViewController *viewController;
And then in AppDelegate.m I do this :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
ViewController *viewController = [[ViewController alloc] init];
viewController.title = #"Agrinuvo";
[navigation pushViewController:viewController animated:NO];
navigation = [[UINavigationController alloc] initWithRootViewController:viewController];
navigation.navigationBar.tintColor = [UIColor darkGrayColor];
[[UIBarButtonItem appearance] setTintColor:[UIColor orangeColor]];
[_window addSubview:navigation.view];
}
I have tried in my ViewController.m viewDidLoad method these :
self.navigationController.navigationBar.autoresizesSubviews = YES;
self.navigationController.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
I am able to resize the UINavigationBar but I cannot rotate it for the life of me (still in ViewController.m) :
-(void) orientationChanged
{
CGRect frame = self.navigationController.navigationBar.frame;
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait)
{
self.view.transform = CGAffineTransformMakeRotation(0);
frame.size.height = 44;
frame.origin.y = 15;
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown)
{
self.view.transform = CGAffineTransformMakeRotation(M_PI * 1);
frame.size.height = 44;
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
{
self.view.transform = CGAffineTransformMakeRotation(M_PI * -0.5);
frame.size.height = 32;
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft)
{
self.view.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
frame.size.height = 32;
}
self.navigationController.navigationBar.frame = frame;
}
Ah yes, I also tried this method in all of my controllers and it is returning YES in all of them but the NavigationBar still does not rotate.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
So I'm wondering if it is actually possible to rotate a NavigationBar. Also do I create it like it should be created?
Should I be initiating my UINavigationController this way or should I be creating it in IB and link it to a .h class?
Sorry for the long read and thank you for any help you can provide
You should make your navigation controller the root view controller of the window. Also, in your applicationDidBecomeActive:method, you have this line: [navigation pushViewController:viewController animated:NO]; which you use before you've instantiated navigation, so that line isn't doing anything.

How can I change navigationBar Background

I try change to background of navigationBar.
Search.h:
#import <UIKit/UIKit.h>
#interface Search : UIViewController
#end
Search.m:
#import "Search.h"
#implementation Search
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *image = [UIImage imageNamed:#"navigation_header.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
I can't change navigation background.
Thanks.
This is actually simpler to do then you think. In your app delegate...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"NavBar.png"] forBarMetrics:UIBarMetricsDefault];
return YES;
}
This will universally change your navigation bar background image.
Note: I believe UINavigationBar's "appearance" property is available in iOS 5 and up.
viewController.h
IBOutlet UINavigationBar *navBar;
viewController.m
//for color
- (void)viewDidLoad{
[super viewDidLoad];
[navBar setTintColor:[UIColor colorWithRed:0.0/255.0 green:100.0/255.0 blue:20.0/255.0 alpha:1.0]];
}
//for image
UIImage *image = [UIImage imageNamed:#"navigation_header.png"];
[self.navigationController.navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[Search alloc] initWithNibName:#"Search" bundle:nil] autorelease];
navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:[navControl view]];
[self.window makeKeyAndVisible];
}
Write this method in AppDelegate.m. I think it will be helpful to you.

UITextField Example in Cocos2d

Can anyone please suggest some links for using UITextField in cocos2d.
I want to press on label, then the UITextField should get selected and I need to edit on that UITextField.
I'm doing this in a current project to allow for entering the number of the level to start playing at, so that's why my variables and methods are named the way they are; you should probably adjust these to make sense for you.
In your app controller, define this as an instance variable:
UITextField *levelEntryTextField;
Create it inside applicationDidFinishLaunching:
levelEntryTextField = [[UITextField alloc] initWithFrame:
CGRectMake(60, 165, 200, 90)];
[levelEntryTextField setDelegate:self];
Define a method to activate the text field. You should also declare it in the header file for your app controller.
- (void)specifyStartLevel
{
[levelEntryTextField setText:#""];
[window addSubview:levelEntryTextField];
[levelEntryTextField becomeFirstResponder];
}
This will make pressing "return" on the keypad end editing
- (BOOL)textFieldShouldReturn:(UITextField*)textField {
//Terminate editing
[textField resignFirstResponder];
return YES;
}
This is triggered when the editing is actually done.
- (void)textFieldDidEndEditing:(UITextField*)textField {
if (textField==levelEntryTextField) {
[levelEntryTextField endEditing:YES];
[levelEntryTextField removeFromSuperview];
// here is where you should do something with the data they entered
NSString *result = levelEntryTextField.text;
}
}
Now to actually set things in motion, you put this somewhere. I call this from within one of my Scene classes, in response to a user action:
[[[UIApplication sharedApplication] delegate] specifyStartLevel];
I took the example that Jack provided and actually created a working project, this was done using the Cocos2D 0.7.1 XCode Template, and then just editting the *AppDelegate.m/.h files, which are provided below in there entirety. I also modified some of what Jack said, because I feel that creating the UITextField in the appDidFinishLoading would utilize a bit too much memory, especially if the text field is not used all the time ... this solution creates the text field only when it is needed, the sample draws an empty Cocos2D Layer scene, and on screen touch, it displays the text field for you to start entering text into. It will spit out the result of what you entered to the Console - you can pass this to whatever is necessary in your own code.
the .h
#import <UIKit/UIKit.h>
#import "cocos2d.h"
#interface MYSCENE : Layer <UITextFieldDelegate>
{
UITextField *myText;
}
-(void)specificStartLevel;
#end
#interface textFieldTestAppDelegate : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate, UIApplicationDelegate>
{
UIWindow *window;
}
#end
and then the .m
#import "textFieldTestAppDelegate.h"
#implementation MYSCENE
-(id) init
{
self = [super init];
isTouchEnabled = YES;
return self;
}
-(BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self specifyStartLevel];
return kEventHandled;
}
-(void)specifyStartLevel {
myText = [[UITextField alloc] initWithFrame:CGRectMake(60, 165, 200, 90)];
[myText setDelegate:self];
[myText setText:#""];
[myText setTextColor: [UIColor colorWithRed:255 green:255 blue:255 alpha:1.0]];
[[[[Director sharedDirector] openGLView] window] addSubview:myText];
[myText becomeFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[myText resignFirstResponder];
return YES;
}
-(void)textFieldDidEndEditing: (UITextField *)textField {
if(textField == myText) {
[myText endEditing:YES];
[myText removeFromSuperview];
NSString *result = myText.text;
NSLog([NSString stringWithFormat:#"entered: %#", result]);
} else {
NSLog(#"textField did not match myText");
}
}
-(void) dealloc
{
[super dealloc];
}
#end
#implementation textFieldTestAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window setUserInteractionEnabled:YES];
[[Director sharedDirector] setDisplayFPS:YES];
[[Director sharedDirector] attachInWindow:window];
Scene *scene = [Scene node];
[scene addChild: [MYSCENE node]];
[window makeKeyAndVisible];
[[Director sharedDirector] runWithScene: scene];
}
-(void)dealloc
{
[super dealloc];
}
-(void) applicationWillResignActive:(UIApplication *)application
{
[[Director sharedDirector] pause];
}
-(void) applicationDidBecomeActive:(UIApplication *)application
{
[[Director sharedDirector] resume];
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
[[TextureMgr sharedTextureMgr] removeAllTextures];
}
#end
To add Text field in cocos2d as below code
first of all you add view in Scene and afetr add textfield add in view thats very easy.
-(id) init
{
if( (self=[super init]) )
{
// add view in scene
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
view.backgroundColor = [UIColor redColor];
// add textfield in view
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 140, 300, 30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[view addSubview:textField];
// add view in scene
[[[CCDirector sharedDirector] view] addSubview:view];
}
return self;
}
you can also use CCTextfield in cocos2d best example is https://github.com/iNinja/CCTextField
Try the following CCNode subclass, CCMenuItemTextField, to use text fields in cocos2d.
The class is directly subclassed from CCMenuItemSprite. When tapped, the "selected" method is called and a UITextField is added to the main window. After editing is done, "unselected" method is called and the UITextField is removed from screen. User's input is saved to a CCLabelTTF node, which position itself exactly as the original UITextField.
CCMenuItemTextField.h
#interface CCMenuItemTextField : CCMenuItemSprite<UITextFieldDelegate> {
UITextField *textField_;
CCLabelTTF *label_;
CGFloat paddingLeft_;
}
#property (readonly, nonatomic) UITextField *textField;
#property (readonly, nonatomic) CCLabelTTF *label;
#property (assign, nonatomic) CGFloat paddingLeft;
- (void)selected;
- (void)unselected;
- (void)setFontSize:(CGFloat)size;
- (NSString*)text;
- (void)setText:(NSString*)text;
#end
CCMenuItemTextField.m
#import "CCMenuItemTextField.h"
#implementation CCMenuItemTextField
#synthesize
textField = textField_,
label = label_,
paddingLeft = paddingLeft_;
- (id)init
{
CCSprite *normalSprite = [CCSprite spriteWithFile:#"text_field_background.png"];
CCSprite *selectedSprite = [CCSprite spriteWithFile:#"text_field_background.png"];
CCSprite *disabledSprite = [CCSprite spriteWithFile:#"text_field_background.png"];
return [self initWithNormalSprite:normalSprite selectedSprite:selectedSprite disabledSprite:disabledSprite];
}
- (id)initWithNormalSprite:(CCNode<CCRGBAProtocol> *)normalSprite
selectedSprite:(CCNode<CCRGBAProtocol> *)selectedSprite
disabledSprite:(CCNode<CCRGBAProtocol> *)disabledSprite
{
self = [super initWithNormalSprite:normalSprite
selectedSprite:selectedSprite
disabledSprite:disabledSprite
target:self
selector:#selector(selected)];
if (self) {
paddingLeft_ = 3.0;
textField_ = [[UITextField alloc] init];
[textField_ setTextColor:[UIColor blackColor]];
[textField_ setFont:[UIFont systemFontOfSize:18]];
label_ = [[CCLabelTTF node] retain];
[label_ setAnchorPoint:ccp(0,0.5)];
[label_ setFontSize:18];
[label_ setVisible:NO];
[label_ setColor:ccBLACK];
[self addChild:label_];
}
return self;
}
- (void)dealloc
{
[label_ release];
[textField_ release];
[super dealloc];
}
// --------------------------------
// Public
// --------------------------------
- (void)selected
{
[super selected];
[label_ setVisible:NO];
CGAffineTransform transform = [self nodeToWorldTransform];
float textFieldHeight = textField_.font.lineHeight;
float width = self.contentSize.width;
float height = self.contentSize.height;
float left = transform.tx + paddingLeft_;
float top = 480 - transform.ty - height + (height - textFieldHeight) / 2;
[textField_ setFrame:CGRectMake(left, top, width, height)];
[[[[CCDirector sharedDirector] view] window] addSubview:textField_];
[textField_ becomeFirstResponder];
[textField_ setDelegate:self];
}
- (void)unselected
{
[super unselected];
[label_ setVisible:YES];
[label_ setPosition:ccp(paddingLeft_, self.contentSize.height/2)];
NSString *text = textField_.text ? textField_.text : #"";
[label_ setString:text];
[textField_ resignFirstResponder];
[textField_ removeFromSuperview];
}
- (NSString*)text
{
return [label_ string];
}
- (void)setText:(NSString*)text
{
[label_ setString:text];
[textField_ setText:text];
}
// --------------------------------
// UITextFieldDelegate
// --------------------------------
- (BOOL)textFieldShouldReturn:(UITextField*)textField {
[self unselected];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField*)textField {
[self unselected];
}
- (void)setFontSize:(CGFloat)size
{
[label_ setFontSize:size];
[textField_ setFont:[UIFont systemFontOfSize:size]];
}
// --------------------------------
// CCNode
// --------------------------------
- (void)onExitTransitionDidStart
{
[super onExitTransitionDidStart];
[self unselected];
}
#end