本文写的是用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];
}
}