//Author:smilelance
//From:http://blog.csdn.net/smilelance
#import <UIKit/UIKit.h>
@interface PDESegmentControl :
UIView
{
NSMutableArray *segmentButtons;
NSMutableArray *buttonImgNames;
}
@property (readonly,
nonatomic) NSInteger selectedSegmentIndex;
- (id) initWithFrame:(CGRect)frame items:(NSArray*)itemArray;
-(void)setSegmentIndex:(NSInteger)index;
//segment control
// NSArray * segmentItems = [NSArray arrayWithObjects: @"未審批", @"已審批", nil];
// UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems: segmentItems];
// segmentedControl.frame = CGRectMake(310, 295, 160, 36);
// [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
// segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;
// segmentedControl.selectedSegmentIndex = 0;
// myBorrowRequestType = segmentedControl.selectedSegmentIndex;
// [self.view addSubview:segmentedControl];
@end
#import "PDESegmentControl.h"
#define SEGMENT_UNSELECTED 0
#define SEGMENT_SELECTED 1
@implementation PDESegmentControl
- (id)initWithFrame:(CGRect)frame
{
self = [super
initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id) initWithFrame:(CGRect)frame items:(NSArray*)itemArray
{
self = [super
initWithFrame:frame];
if (self) {
int segmentCount = [itemArray
count];
segmentButtons = [[NSMutableArray
alloc] init];
buttonImgNames = [[NSMutableArray
alloc] init];
float segmentWidth = frame.size.width/segmentCount;
for (int i=0; i<segmentCount; i++) {
UIButton *button = [UIButton
buttonWithType:UIButtonTypeCustom];
button.frame =
CGRectMake(segmentWidth*i, 0,
segmentWidth, frame.size.height);
if (i==0) {
//left
[buttonImgNames
addObject:@"seg_btn_left_nor.png"];
[buttonImgNames
addObject:@"seg_btn_left_sel.png"];
}else if(i==segmentCount-1){
//right
[buttonImgNames
addObject:@"seg_btn_right_nor.png"];
[buttonImgNames
addObject:@"seg_btn_right_sel.png"];
}else{ //middle
}
button.tag = i;
[button addTarget:self
action:@selector(segmentAction:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[itemArray
objectAtIndex:i] forState:UIControlStateNormal];
[segmentButtons
addObject:button];
[self addSubview:button];
}
[self
setSegmentIndex:0];
}
return self;
}
-(void)setSegmentIndex:(NSInteger)index
{
_selectedSegmentIndex = index;
[self segmentAction:[segmentButtons
objectAtIndex:index]];
}
-(void)segmentAction:(id)sender
{
UIButton *button = (UIButton*)sender;
int tag = button.tag;
for(int i=0; i<[segmentButtons
count]; i++){
int nameOffset = SEGMENT_UNSELECTED;
if (tag == i) {
nameOffset = SEGMENT_SELECTED;
}
UIButton *segButton = [segmentButtons
objectAtIndex:i];
[segButton setBackgroundImage:[UIImage
imageNamed:[buttonImgNames
objectAtIndex:i*2+nameOffset]]
forState:UIControlStateNormal];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end