Sunday, 16 June 2013

How to make a simple segmented control button programmatically in your cocos2d application

You may have noticed the segmented control button in iPhone and iPad applications. In this tutorial, we will teach you how to make a simple segmented control button programmatically in your cocos2d application.


For creating  the segmented control view, first create an UIView:
 
        UIView *profileView = [[UIView alloc] init];
        profileView.frame = CGRectMake(0, 50, 320, 395);
        profileView.backgroundColor = [UIColor clearColor];
        [[[CCDirector sharedDirector] openGLView] addSubview:profileView];
      
Then create the segmented control label array:
 
        NSArray *itemArray = [NSArray arrayWithObjects: @"First", @"Second", nil];
  
After that, create the UISegmentedControl and add to the UIView:

        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
        segmentedControl.frame = CGRectMake(0, 0, 150, 40);
        segmentedControl.center = CGPointMake(160, 240);
        segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
        segmentedControl.selectedSegmentIndex = 1;
        segmentedControl.tintColor = [UIColor colorWithRed:0.1f green:0.4f  blue:0.65f  alpha:1.0f];
        segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
      
        [segmentedControl addTarget:self
                             action:@selector(segmentChanged:)
                   forControlEvents:UIControlEventValueChanged];
        [profileView addSubview:segmentedControl];

No comments:

Post a Comment