自定义没有圆角的类似UISegmentedControl控件

2016-05-31  本文已影响366人  维子Vanessa
IMG_2255.jpg
//
//  HeadView.h
//  自定义Seg控件
//
//  Created by Vanessa on 16/5/31.
//  Copyright © 2016年 Vanessa. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HeadView : UIView

@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSMutableArray <UIButton *>*buttonArray;
@property (nonatomic, strong) UIColor *tint_Color;
@property (nonatomic, strong) UIFont *textFont;
@property (nonatomic, assign) NSInteger selectedIndex;
@property (nonatomic, strong) UIButton *selectedButton;
@property (nonatomic, strong) id delegate;
@property (nonatomic) SEL action;


-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)event;

@end
//
//  HeadView.m
//  自定义Seg控件
//
//  Created by Vanessa on 16/5/31.
//  Copyright © 2016年 Vanessa. All rights reserved.
//

#import "HeadView.h"

@implementation HeadView

-(instancetype)init {
    
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        _textFont = [UIFont systemFontOfSize:13];
        _tint_Color = [UIColor blackColor];
        _selectedIndex = 0;
        _buttonArray = [NSMutableArray array];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame {
    //初始化默认数据
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        _textFont = [UIFont systemFontOfSize:13];
        _tint_Color = [UIColor blackColor];
        _selectedIndex = 0;
        _buttonArray = [NSMutableArray array];
    }
    return self;
}

static int tagIndex = 151515;
-(void)setTitleArray:(NSArray *)titleArray {
    
    
    _titleArray = titleArray;
    CGFloat width = self.frame.size.width / titleArray.count;
    for (int i = 0; i < _titleArray.count; i++) {
        NSString *string = _titleArray[i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(width * i, 0, width, self.frame.size.height);
        button.tag = tagIndex + i;
        button.titleLabel.font = self.textFont;
        [button setTitle:string forState:UIControlStateNormal];
        [button setTitle:string forState:UIControlStateSelected];
        [button setTitleColor:self.tint_Color forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor]forState:UIControlStateSelected];
        [button addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.borderWidth = 0.5;
        button.layer.borderColor = _tint_Color.CGColor;
        if (i == self.selectedIndex) {
            button.selected = YES;
            self.selectedButton = button;
            [button setBackgroundColor:self.tint_Color];
        }
        [self.buttonArray addObject:button];
        [self addSubview:button];
    }
}

-(void)setTint_Color:(UIColor *)tint_Color {
    
    _tint_Color = tint_Color;
    for (UIButton *button in self.subviews) {
        if ([button isKindOfClass:[UIButton class]]) {
            [button setTitleColor:tint_Color forState:UIControlStateNormal];
            button.layer.borderColor = tint_Color.CGColor;
        }
        if (button.selected == YES) {
            [button setBackgroundColor:tint_Color];
        }
    }
}

-(void)setTextFont:(UIFont *)textFont {
    
    _textFont = textFont;
    for (UIButton *button in self.subviews) {
        if ([button isKindOfClass:[UIButton class]]) {
            button.titleLabel.font = textFont;
        }
    }
}

-(void)setSelectedIndex:(NSInteger)selectedIndex {
    
    _selectedIndex = selectedIndex;
    [self didClickButton:self.buttonArray[selectedIndex]];
}

-(void)didClickButton:(UIButton *)button {
    
    _selectedIndex = button.tag - tagIndex;
    [self.selectedButton setBackgroundColor:[UIColor whiteColor]];
    self.selectedButton.selected = NO;
    self.selectedButton = button;
    self.selectedButton.selected = YES;
    [self.selectedButton setBackgroundColor:self.tint_Color];
    if (!self.delegate || !_action) {
        return;
    }
    [self.delegate performSelector:_action withObject:self];
}

-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)event{
    
    if (event == UIControlEventValueChanged) {
        self.delegate = target;
        self.action = action;
    }
}
@end

具体使用步骤:

//添加控件
HeadView *headView = [[HeadView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 44)];
    headView.titleArray = @[@"未完成",@"已完成"];
    headView.selectedIndex = self.currentIndex;//设置默认选择项索引
    headView.tint_Color = [UIColor lightGrayColor];
    [headView addTarget:self action:@selector(didclickHeadview:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:headView];

#pragma mark - Event
-(void)didclickHeadview:(HeadView *)headview {
 
    NSLog(@"%s---%zd",__func__,headview.selectedIndex);
    
}

http://pan.baidu.com/s/1hrN6USs

上一篇下一篇

猜你喜欢

热点阅读