UISegment 自定义


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



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值