自定义segmentControl

#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

//
//  PDESegmentControl.m
//  xxx
//
//  Created by Tony on 15/4/2.
//  Copyright (c) 2015年 Tony. All rights reserved.
//

#import "PDESegmentControl.h"

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值