用Button子类化封装假的分段控件

本文写的是用button封装一个继承自UI view的控件,达到的效果就是看上去是分段控件,但是可以自定义不同状态下各个button的背景图片,可以设置是否需要下划线标识,采用代理的方式,让控制器拿到当前选中的button的下标进行接下来的操作。

首先,我在继承自UI view的类中定义了一个数组:控件标题的数组,然后定义了set方法。因为我要设置button的图片,所以,还定义了normal和selected状态下的图片名数组,还有是否需要下划线标识属性

@interface CustomContrlBtnView : UIView
{
    NSArray *_menuArray;//标题数组

    //是否需要底部划线
    BOOL _isNeedLine;
}

//正常状态的背景图片
@property (nonatomic, strong) NSArray *imgArr;

//选中时的背景图片
@property (nonatomic, strong) NSArray *selectImgArr;

//是否需要底部划线
@property BOOL isNeedLine;

//delegate
@property (nonatomic, strong)id <MenuProtocol> myDelegate;

- (void)setNameWithArray:(NSArray *)menuArray;  //set 方法

bool类型的属性定义还要在.m文件中写入:

@synthesize isNeedLine = _isNeedLine;

代理要遵循协议,所以定义一个协议

//协议
@protocol MenuProtocol <NSObject>

@optional //可选的

@required //必选的

- (void)getTag:(NSInteger)tag;//获取当前选中下标

@end

然后,在set方法的实现方法中,分别创建button,设置button的frame:

- (void)setNameWithArray:(NSArray *)menuArray
{
    _menuArray = menuArray;

    //计算每个button的宽度
    CGFloat btnW = self.frame.size.width / [_menuArray count];
    for (int i = 0; i < menuArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i * btnW, 0, btnW, self.frame.size.height);

        btn.backgroundColor = [UIColor clearColor];

        //设置tag值
        btn.tag = 1501 + i;

        //设置默认第一个选中
        if (btn.tag == 1501) {

            btn.selected = YES;

        }

        //判断是否有背景图片
        if (self.imgArr.count > 0) {

            //设置背景图片
            [btn setBackgroundImage:[UIImage imageNamed:self.imgArr[i]] forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageNamed:self.selectImgArr[i]] forState:UIControlStateSelected];
        }

       //根据有无下划线确定按钮颜色
        if (_isNeedLine) {

            //设置按钮的字体大小 颜色 状态
            [btn setTitleColor:kContrlColor forState:UIControlStateNormal];
            [btn setTitleColor:kItemColor forState:UIControlStateSelected];

        }else {

            //设置按钮的字体大小 颜色 状态
            [btn setTitleColor:kskyBlueColor forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        }

        //设置标题
        [btn setTitle:_menuArray[i] forState:UIControlStateNormal];

        //添加点击事件
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:btn];
    }

    //创建底部划线(先判断是否有划线)
    if (_isNeedLine) {
        UIView *markLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 2, btnW, 2)];
        markLine.tag = 999;
        markLine.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"矩形-2-3.png"]];
        [self addSubview:markLine];
    }


}

点击事件触发的方法中,改变selected状态,若有下划线标识,动画移动,然后触发代理方法即可:

- (void)btnClick:(UIButton *)button
{
    //遍历子视图数组,把选中button的选中值设为yes,其他设为no
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            UIButton *subBtn = (UIButton *)subView;
            if (subBtn.tag == button.tag) {

                [subBtn setSelected:YES];

            }else {

                [subBtn setSelected:NO];
            }
        }
    }

    if (_isNeedLine) {
        //动画移动底部划线(判断)
        UIView *markView = (UIView *)[self viewWithTag:999];

        [UIView animateWithDuration:0.2f animations:^{
            markView.frame = CGRectMake((button.tag - 1501) * (button.frame.size.width), self.frame.size.height - 2, button.frame.size.width, 2);
        }];
    }

    //实现代理方法
    if ([self.myDelegate respondsToSelector:@selector(getTag:)]) {
        [self.myDelegate getTag:button.tag];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值