zoukankan      html  css  js  c++  java
  • Ios使用按钮自定义segmentcontrol

    //Author:smilelance

    //From:http://blog.csdn.net/smilelance

    #import <UIKit/UIKit.h>


    @interface PDESegmentControl : UIView

    {

        NSMutableArray *segmentButtons;

        NSMutableArray *buttonImgNames;

    }

    @property (readonly, nonatomicNSInteger 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


  • 相关阅读:
    word 中如何取消格式标记
    linux 之 汇编语言 的mov和movl sub 和subl add 和addl 的区别??
    Linux 之 AT&T汇编语言 mov、add、sub指令、数据段
    D65光源
    802.11 ------ 简介
    802.11 ------ Beacon帧、Beacon Interval、TBTT、Listen Interval、TIM、DTIM
    NAT ------ 为什么手动设置NAT端口映射(转发)不成功,导致访问不了局域网服务器
    UDP ------ UDP 详解
    UDP ------ UDP IPPROTO_UDPLITE 参数
    GNU C ------ __attribute__
  • 原文地址:https://www.cnblogs.com/secbook/p/2655370.html
Copyright © 2011-2022 走看看