// code
//
// CustomSegment.h
// Learn3
//
// Created by lance on 14-4-11.
// Copyright (c) 2014年 Lance. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface CustomSegment : UIControl
@property (nonatomic, strong) NSArray *items;
@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, strong) UIColor *borderColor; // 边界颜色
@property (nonatomic, strong) UIColor *selectBgColor; // 选中背景色
@property (nonatomic, strong) UIColor *bgColor; // 背景色
@property (nonatomic, strong) UIColor *selectTitleColor; // 选中标题颜色
@property (nonatomic, strong) UIColor *titleColor; // 标题颜色
- (id)initWithFrame:(CGRect)frame items:(NSArray *)items;
@end
//
// CustomSegment.m
// Learn3
//
// Created by lance on 14-4-11.
// Copyright (c) 2014年 Lance. All rights reserved.
//
#import "CustomSegment.h"
@implementation CustomSegment
{
NSMutableArray *_itemsViewArray;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)initWithFrame:(CGRect)frame items:(NSArray *)items
{
self = [super initWithFrame:frame];
if (self) {
_items = items;
[self initViews];
}
return self;
}
- (void)initViews
{
_itemsViewArray = [[NSMutableArray alloc] initWithCapacity:_items.count];
// item 宽度
float itemWidth = self.frame.size.width / _items.count;
// 判断是否设置颜色,没有设置,则默认颜色
if (!_borderColor) {
_borderColor = [UIColor colorWithRed:248.0 / 255.0 green:154.0 / 255.0 blue:15.0 / 255.0 alpha:1.0];
}
if (!_selectBgColor) {
_selectBgColor = [UIColor colorWithRed:248.0 / 255.0 green:154.0 / 255.0 blue:15.0 / 255.0 alpha:1.0];
}
if (!_bgColor) {
_bgColor = [UIColor whiteColor];
}
if (!_selectTitleColor) {
_selectTitleColor = [UIColor whiteColor];
}
if (!_titleColor) {
_titleColor = [UIColor colorWithRed:248.0 / 255.0 green:154.0 / 255.0 blue:15.0 / 255.0 alpha:1.0];
}
// 创建item UI
for (int i = 0; i < [_items count]; i ++) {
NSString *itemName = [_items objectAtIndex:i];
UIView *itemView = [[UIView alloc] initWithFrame:CGRectMake(itemWidth * i, 0.0, itemWidth, CGRectGetHeight(self.frame))];
itemView.backgroundColor = [UIColor whiteColor];
// 标题
UILabel *titleLabel = [[UILabel alloc] initWithFrame:itemView.bounds];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = _titleColor;
titleLabel.text = itemName;
titleLabel.tag = 2013;
[itemView addSubview:titleLabel];
// 默认选中
if (i == _selectIndex) {
itemView.backgroundColor = _selectBgColor;
titleLabel.textColor = _selectBgColor;
}
// 竖线
if (i != [_items count] - 1) {
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(itemView.frame) - 1, 0.0, 1.0, CGRectGetHeight(self.frame))];
lineView.backgroundColor = _borderColor;
[itemView addSubview:lineView];
}
// 第一个和最后一个圆角
if (i == 0 || i == [_items count] - 1) {
itemView.layer.cornerRadius = 2.0;
itemView.layer.masksToBounds = YES;
}
[self addSubview:itemView];
[_itemsViewArray addObject:itemView];
}
// 整个视图border
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 6;
self.layer.borderWidth = 1.0;
self.layer.borderColor = _borderColor.CGColor;
}
- (void)setSelectIndex:(NSInteger)selectIndex
{
_selectIndex = selectIndex;
// 更新选中与未选中界面
for (int i = 0; i < [_items count]; i ++) {
UIView *itemView = [_itemsViewArray objectAtIndex:i];
UILabel *titleLabel = (UILabel *)[itemView viewWithTag:2013];
if (i == selectIndex) {
itemView.backgroundColor = _selectBgColor;
titleLabel.textColor = _selectTitleColor;
} else {
itemView.backgroundColor = _bgColor;
titleLabel.textColor = _titleColor;
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
float width = self.frame.size.width / [self.items count];
self.selectIndex = point.x / width;
// 事件传递
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
@end