Sunday, July 29, 2012

Adding Box2D Files to your Cocos2D Project

  1. First, download 'cocos2d-iphone-x.0.tar.gz' , and Open a Finder window and navigate to where you downloaded the Cocos2D source.
  2. Open the folder and navigate to external\Box2d\Box2D. 
  3. Open a second Finder window, navigate to your project, and navi-
    gate to libs. 
  4. Copy the Box2D folder to the libs folder. 

  • Adding the Box2D directory to your Xcode project   

  • You have to set "User Header Search Paths" as 'Project_Name/libs'

  • And also you have to add two more files :
    the ones responsible for drawing Box2D objects to the screen for debug purposes.

  navigate to your Cocos2D directory, and navigate to templates\cocos2d_box2d_app\Classes. Inside you’ll find the files GLES- Render.h and Select those two files and drag them to your new Box2D group in Xcode. Make sure Copy items into destination group’s folder (if needed) is checked, and click Finish.

  • After that  #import "Box2D.h", what ever file you want and mack sure rename the file with a '.mm' extension. Because you are importing the Box2D header file (which uses C++) in files that are set up to use only Objective-C. To instruct the compiler to allow both C++ and Objective-C, we can add '.mm' extension.  

Wednesday, July 25, 2012

How to use DatePicker in iOS

UIDatePicker *_datePickerView = [[UIDatePicker alloc] initWithFrame:CGRectZero];
            _datePickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
            _datePickerView.datePickerMode = UIDatePickerModeDate;
            CGRect pickerRect = CGRectMake(    0.0, 480, 320, 250);
            _datePickerView.frame = pickerRect;
            _datePickerView.hidden = YES;
            _datePickerView.datePickerMode = UIDatePickerModeDate;
            // in case we previously chose the Counter style picker, make sure
            // the current date is restored
            NSDate *today = [NSDate date];
   = today;
            _datePickerView.datePickerMode = UIDatePickerModeDate;
            [_datePickerView addTarget:self action:@selector(updateLabel:) forControlEvents:UIControlEventValueChanged];
            [parent.view addSubview:_datePickerView];

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterMediumStyle;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int year   =    [[calendar components:NSYearCalendarUnit    fromDate:[_datePickerView date]] year];
    int month  =    [[calendar components:NSMonthCalendarUnit   fromDate:[_datePickerView date]] month];
    int day    =    [[calendar components:NSDayCalendarUnit     fromDate:[_datePickerView date]] day];
    NSString *date = [NSString stringWithFormat:@"%d/%d/%d",year, month, day];//[df]];
    [_birthdayLabel setTitle:date forState:UIControlStateNormal];
    /* the following allows you to choose the date components
     NSCalendar *calendar = [NSCalendar currentCalendar];
     int hour   =    [[calendar components:NSHourCalendarUnit    fromDate:[datePicker date]] hour];
     int minute =    [[calendar components:NSMinuteCalendarUnit  fromDate:[datePicker date]] minute];
     int year   =    [[calendar components:NSYearCalendarUnit    fromDate:[datePicker date]] year];
     int month  =    [[calendar components:NSMonthCalendarUnit   fromDate:[datePicker date]] month];
     int day    =    [[calendar components:NSDayCalendarUnit     fromDate:[datePicker date]] day];

How to add left padding to the UITextField, when add background Image

#import "QuartzCore/QuartzCore.h"    // First import this Framework

UITextField *_emailText = [[UITextField alloc]initWithFrame:CGRectMake(40, 8, 250, 34)];
//    _emailText.borderStyle = UITe;  // Please set borderStyle to default value, otherwise it is not working.
    _emailText.delegate = self;
    _emailText.placeholder = @"Email";
    [_emailText setFont:[UIFont boldSystemFontOfSize:12]];
    _emailText.tag = 1;
    _emailText.returnKeyType = UIReturnKeyNext;
    _emailText.textAlignment = UITextAlignmentLeft;
    _emailText.keyboardType = UIKeyboardTypeEmailAddress;
    _emailText.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _emailText.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
    _emailText.leftViewMode = UITextFieldViewModeAlways;
    _emailText.background = [[UIImage imageNamed:@"textBox.png"] stretchableImageWithLeftCapWidth:7 topCapHeight:17];
    [self.view addSubview:_emailText];