TabsView.h
#import <UIKit/UIKit.h>
@interface TabsView : UIView
@property (nonatomic,copy)void (^onClickTitle)(UIButton *btn,NSInteger currentIndex,NSInteger toIndex);
//初始化
- (id)initWithFrame:(CGRect)frame WithTitleArr:(NSArray *)titleAr ;
//初始化某個title
- (void)initTitle:(NSInteger)toIndex ;
@end
TabsView.m
#import "TabsView.h"
#define TAG_START 1000
#define BTNHIGHSPACE 8
#define BTNWIDTHSPACE 12
#define BTNBASECOLOR [UIColor colorWithRed:217.0 / 255 green:98.0 / 255 blue:76.0 / 255 alpha:1.0]
@interface TabsView()
@property (nonatomic,strong)NSMutableArray *btnArr;
@property (nonatomic,assign)NSInteger currentIndex; //當(dāng)前顯示頁的索引
@end
@implementation TabsView
//初始化
- (id)initWithFrame:(CGRect)frame WithTitleArr:(NSArray *)titleAr
{
self = [super initWithFrame:frame];
if (self) {
_currentIndex = -1;
//title的數(shù)量
NSInteger titleCount = titleAr.count;
//基座:
UIView *btnBaseView = [[UIView alloc]initWithFrame:CGRectMake(BTNWIDTHSPACE, BTNHIGHSPACE, self.frame.size.width - 2 * BTNWIDTHSPACE, self.frame.size.height -2*BTNHIGHSPACE)];
btnBaseView.backgroundColor = BTNBASECOLOR;
btnBaseView.layer.cornerRadius = 4;
btnBaseView.layer.borderColor = BTNBASECOLOR.CGColor;
btnBaseView.layer.borderWidth = 1;
[self addSubview:btnBaseView];
_btnArr = [[NSMutableArray alloc]initWithCapacity:titleCount];
for (int i=0; i < titleCount; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//計(jì)算尺寸及位置:
CGFloat width = (btnBaseView.width-2-1*(titleCount-1)) / titleCount;
CGFloat height = self.frame.size.height - 2 * BTNHIGHSPACE - 2;
CGFloat x = BTNWIDTHSPACE + 1 + i * (width + 1);
CGFloat y = BTNHIGHSPACE + 1;
btn.frame = CGRectMake(x, y, width, height);
btn.tag = TAG_START + i;//tag
btn.titleLabel.font = [UIFont systemFontOfSize:16];
//設(shè)置文字及字體顏色:
[btn setTitle:[titleAr objectAtIndex:i] forState:UIControlStateNormal];
[btn setTitleColor:BTNBASECOLOR forState:UIControlStateNormal];
//第一個按鈕挤渐,左側(cè)圓角:
if (i == 0)
{
UIBezierPath *maskSalePath = [UIBezierPath bezierPathWithRoundedRect:btn.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(3, 3)];
CAShapeLayer *maskSaleLayer = [[CAShapeLayer alloc] init];
maskSaleLayer.frame = btn.bounds;
maskSaleLayer.path = maskSalePath.CGPath;
btn.layer.mask = maskSaleLayer;
}
//最后一個按鈕闽烙,右側(cè)圓角:
if (i == (titleCount - 1))
{
UIBezierPath *maskTerminalPath = [UIBezierPath bezierPathWithRoundedRect:btn.bounds byRoundingCorners:UIRectCornerBottomRight | UIRectCornerTopRight cornerRadii:CGSizeMake(3, 3)];
CAShapeLayer *maskTerminalLayer = [[CAShapeLayer alloc] init];
maskTerminalLayer.frame = btn.bounds;
maskTerminalLayer.path = maskTerminalPath.CGPath;
btn.layer.mask = maskTerminalLayer;
}
/*
//如果不是最后一個按鈕,需要添加一個分隔豎線
if (i < (titleCount - 1)) {
//計(jì)算尺寸及位置
CGFloat x = BTNWIDTHSPACE + (i+1)*(btnBaseView.width/titleCount);
CGFloat y = BTNHIGHSPACE+1;
CGFloat width = 1;
CGFloat height = self.frame.size.height - 2 * BTNHIGHSPACE - 2;
UIView *splitLine = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
splitLine.backgroundColor = BTNBASECOLOR;
[self addSubview:splitLine];
}
*/
//點(diǎn)擊事件
[btn addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];
[_btnArr addObject:btn];
[self addSubview:btn];
}
}
return self;
}
//按鈕點(diǎn)擊事件:
- (void)didClickBtn:(UIButton *)sender
{
NSInteger toIndex =sender.tag - TAG_START;
if (self.currentIndex == toIndex)
{
return;
}
[self.btnArr enumerateObjectsUsingBlock:^(UIButton *btn, NSUInteger idx, BOOL * _Nonnull stop) {
//當(dāng)前點(diǎn)擊的按鈕:
if (idx == toIndex)
{
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn setBackgroundColor:BTNBASECOLOR];
}
//其他按鈕
else
{
[btn setTitleColor:BTNBASECOLOR forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor whiteColor]];
}
}];
//點(diǎn)擊事件:
self.onClickTitle(sender, self.currentIndex, toIndex);
self.currentIndex = toIndex;
}
//初始化某個title
- (void)initTitle:(NSInteger)toIndex
{
[self.btnArr enumerateObjectsUsingBlock:^(UIButton *btn, NSUInteger idx, BOOL * _Nonnull stop) {
//當(dāng)前點(diǎn)擊的按鈕:
if (idx == toIndex)
{
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn setBackgroundColor:BTNBASECOLOR];
}
//其他按鈕:
else
{
[btn setTitleColor:BTNBASECOLOR forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor whiteColor]];
}
}];
self.currentIndex = toIndex;
}
@end