自定义 UISegmentControl

//
// CustomSegmentedViewController.m
// CustomSegmented
//
// Created by qian cheng on 12-1-12.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "CustomSegmentedViewController.h"

#import "CQSegmentControl.h"

@implementation CustomSegmentedViewController


/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];

// Image And title
NSArray *titleItems = [NSArray arrayWithObjects:NSLocalizedStringFromTable(@"Boy", @"XXXX...这就不写了", nil),
NSLocalizedStringFromTable(@"Girl", @"XXXX...这就不写了", nil), nil];


UIImage *normal_left = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"left_normal" ofType:@"png"]];
UIImage *normal_right = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"right_normal" ofType:@"png"]];

NSMutableArray *unselectImages = [[NSMutableArray alloc]initWithObjects:normal_left, normal_right, nil];
[normal_left release];
[normal_right release];

UIImage *select_left = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"left_press" ofType:@"png"]];
UIImage *select_right = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"right_press" ofType:@"png"]];

NSMutableArray *selectImages = [[NSMutableArray alloc]initWithObjects: select_left, select_right, nil];
[select_left release];
[select_right release];

CQSegmentControl *_segmentedControl= [[CQSegmentControl alloc] initWithItemsAndStype:titleItems stype:TitleAndImageSegmented];
for (UIView *subView in _segmentedControl.subviews)
{
[subView removeFromSuperview];
}
_segmentedControl.normalImageItems = unselectImages;
[unselectImages release];

_segmentedControl.highlightImageItems = selectImages;

[selectImages release];

_segmentedControl.selectedSegmentIndex = 1;

_segmentedControl.frame = CGRectMake(100.0f, 80.0f, 130.0f, 30.0f);
_segmentedControl.selectedItemColor = [UIColor whiteColor];
_segmentedControl.unselectedItemColor = [UIColor grayColor];

[_segmentedControl addTarget:self action:@selector(test:) forControlEvents:UIControlEventValueChanged];
_segmentedControl.font = [UIFont systemFontOfSize:14];
[self.view addSubview:_segmentedControl];
[_segmentedControl release];

//Title
NSArray *titleItem = [NSArray arrayWithObjects:NSLocalizedStringFromTable(@"Boy", @"XXXX...这就不写了", nil),
NSLocalizedStringFromTable(@"Girl", @"XXXX...这就不写了", nil), nil];


CQSegmentControl *_segmented= [[CQSegmentControl alloc] initWithItemsAndStype:titleItem stype:NormalSegmented];
for (UIView *subView in _segmented.subviews)
{
[subView removeFromSuperview];
}
_segmented.selectedSegmentIndex = 1;

_segmented.frame = CGRectMake(100.0f, 180.0f, 130.0f, 30.0f);
_segmented.selectedItemColor = [UIColor whiteColor];
_segmented.unselectedItemColor = [UIColor blueColor];

[_segmented addTarget:self action:@selector(test:) forControlEvents:UIControlEventValueChanged];
_segmented.font = [UIFont systemFontOfSize:14];
[self.view addSubview:_segmented];
[_segmented release];


//image
UIImage *normal_left_1 = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"left_normal" ofType:@"png"]];
UIImage *normal_right_2 = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"right_normal" ofType:@"png"]];

NSMutableArray *test = [[NSMutableArray alloc]initWithObjects:normal_left, normal_right, nil];
[normal_left_1 release];
[normal_right_2 release];


CQSegmentControl *_segmented_test= [[CQSegmentControl alloc] initWithItemsAndStype:test stype:NormalSegmented];
[test release];
for (UIView *subView in _segmented_test.subviews)
{
[subView removeFromSuperview];
}

_segmented_test.selectedSegmentIndex = 1;

_segmented_test.frame = CGRectMake(100.0f, 280.0f, 130.0f, 30.0f);
_segmented_test.selectedItemColor = [UIColor yellowColor];
_segmented_test.unselectedItemColor = [UIColor orangeColor];

[_segmented_test addTarget:self action:@selector(test:) forControlEvents:UIControlEventValueChanged];
_segmented_test.font = [UIFont systemFontOfSize:14];
[self.view addSubview:_segmented_test];
[_segmented_test release];

}

- (void)test:(id)sender
{
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值