Ios使用按鈕自訂segmentcontrol

來源:互聯網
上載者:User

//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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.