******************************************************************************
baseView.h
#import
<UIKit/UIKit.h>
typedef void(^ClickBlock)(UIButton *);
@interface baseView : UIView
{
BOOL isSelected;
}
@property(nonatomic, copy)ClickBlock block;
@property (nonatomic, retain)UIButton *btn;
@property (nonatomic, retain)UIView *smallLineV;
@property (nonatomic, retain)NSArray *titleArr;
-(instancetype)initWithFrame:(CGRect)frame titleArr : (NSArray *)titleArr;
- (void)addBlock:(ClickBlock)block;
typedef void(^ClickBlock)(UIButton *);
@interface baseView : UIView
{
BOOL isSelected;
}
@property(nonatomic, copy)ClickBlock block;
@property (nonatomic, retain)UIButton *btn;
@property (nonatomic, retain)UIView *smallLineV;
@property (nonatomic, retain)NSArray *titleArr;
-(instancetype)initWithFrame:(CGRect)frame titleArr : (NSArray *)titleArr;
- (void)addBlock:(ClickBlock)block;
@end
******************************************************************************
baseView.m
#import
"baseView.h"
#import "UIViewExt.h"
// 获取设备屏幕的物理尺寸
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
@implementation baseView
-(instancetype)initWithFrame:(CGRect)frame titleArr : (NSArray *)titleArr
{self = [super initWithFrame:frame];
if (self) {
self.titleArr = titleArr;
[self _initViews];
}
return self;
}
-(void)_initViews
{
for (int i = 0; i < _titleArr.count ; i ++) {
self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.frame = CGRectMake(kScreenWidth/_titleArr.count * i, 0, kScreenWidth/_titleArr.count, self.height - 1);
_btn.tag = i;
[_btn setTitle:_titleArr[i] forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor colorWithRed:0.66f green:0.66f blue:0.66f alpha:1.00f] forState:UIControlStateNormal];
[_btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_btn];
}
_smallLineV = [[UIView alloc]initWithFrame:CGRectMake(0, self.height , kScreenWidth/_titleArr.count, 1)];
_smallLineV.backgroundColor = [UIColor colorWithRed:0.24f green:0.25f blue:0.23f alpha:1.00f];
[self addSubview:_smallLineV];
UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
lineV.top = self.height - 1;
lineV.backgroundColor = [UIColor colorWithRed:0.95f green:0.95f blue:0.95f alpha:1.00f];
[self addSubview:lineV];
}
- (void)addBlock:(ClickBlock)block
{
[_btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
self.block = block;
}
#pragma mark 按钮点击事件
-(void)btnAction : (UIButton *)btn
{
if (self.block != nil) {
self.block(btn);
}
_smallLineV.frame = CGRectMake(kScreenWidth/_titleArr.count *btn.tag, self.height , kScreenWidth/_titleArr.count, 1);
for(id cc in [self subviews])
{
if([cc isKindOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)cc;
[btn setTitleColor:[UIColor colorWithRed:0.66f green:0.66f blue:0.66f alpha:1.00f] forState:UIControlStateNormal];
}
}
isSelected = !isSelected;
if (!isSelected) {
[btn setTitleColor:[UIColor colorWithRed:0.66f green:0.66f blue:0.66f alpha:1.00f] forState:UIControlStateNormal];
}else
{
[btn setTitleColor:[UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:1.00f] forState:UIControlStateNormal];
isSelected = NO ;
}
#import "UIViewExt.h"
// 获取设备屏幕的物理尺寸
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
@implementation baseView
-(instancetype)initWithFrame:(CGRect)frame titleArr : (NSArray *)titleArr
{self = [super initWithFrame:frame];
if (self) {
self.titleArr = titleArr;
[self _initViews];
}
return self;
}
-(void)_initViews
{
for (int i = 0; i < _titleArr.count ; i ++) {
self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.frame = CGRectMake(kScreenWidth/_titleArr.count * i, 0, kScreenWidth/_titleArr.count, self.height - 1);
_btn.tag = i;
[_btn setTitle:_titleArr[i] forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor colorWithRed:0.66f green:0.66f blue:0.66f alpha:1.00f] forState:UIControlStateNormal];
[_btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_btn];
}
_smallLineV = [[UIView alloc]initWithFrame:CGRectMake(0, self.height , kScreenWidth/_titleArr.count, 1)];
_smallLineV.backgroundColor = [UIColor colorWithRed:0.24f green:0.25f blue:0.23f alpha:1.00f];
[self addSubview:_smallLineV];
UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
lineV.top = self.height - 1;
lineV.backgroundColor = [UIColor colorWithRed:0.95f green:0.95f blue:0.95f alpha:1.00f];
[self addSubview:lineV];
}
- (void)addBlock:(ClickBlock)block
{
[_btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
self.block = block;
}
#pragma mark 按钮点击事件
-(void)btnAction : (UIButton *)btn
{
if (self.block != nil) {
self.block(btn);
}
_smallLineV.frame = CGRectMake(kScreenWidth/_titleArr.count *btn.tag, self.height , kScreenWidth/_titleArr.count, 1);
for(id cc in [self subviews])
{
if([cc isKindOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)cc;
[btn setTitleColor:[UIColor colorWithRed:0.66f green:0.66f blue:0.66f alpha:1.00f] forState:UIControlStateNormal];
}
}
isSelected = !isSelected;
if (!isSelected) {
[btn setTitleColor:[UIColor colorWithRed:0.66f green:0.66f blue:0.66f alpha:1.00f] forState:UIControlStateNormal];
}else
{
[btn setTitleColor:[UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:1.00f] forState:UIControlStateNormal];
isSelected = NO ;
}
}
@end
******************************************************************************
ViewController.h
#import
<UIKit/UIKit.h>
@interface ViewController :
UIViewController
@end
******************************************************************************
ViewController.m
#import
"ViewController.h"
#import "baseView.h"
// 获取设备屏幕的物理尺寸
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *arr = @[@"你好",@"是的",@"不是的"];
__block ViewController *this = self;
baseView *baseV = [[baseView alloc]initWithFrame:CGRectMake(0, 44, kScreenWidth, 30) titleArr:arr];
[baseV addBlock:^(UIButton *btn) {
[this text:btn];
}];
[self.view addSubview:baseV];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)text : (UIButton*)btn
{
//点击按钮执行的方法
switch (btn.tag) {
case 0:
{
NSLog(@"1111");
}
break;
case 1:
{
NSLog(@"2222");
}
break;
case 2:
{
NSLog(@"333");
}
#import "baseView.h"
// 获取设备屏幕的物理尺寸
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *arr = @[@"你好",@"是的",@"不是的"];
__block ViewController *this = self;
baseView *baseV = [[baseView alloc]initWithFrame:CGRectMake(0, 44, kScreenWidth, 30) titleArr:arr];
[baseV addBlock:^(UIButton *btn) {
[this text:btn];
}];
[self.view addSubview:baseV];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)text : (UIButton*)btn
{
//点击按钮执行的方法
switch (btn.tag) {
case 0:
{
NSLog(@"1111");
}
break;
case 1:
{
NSLog(@"2222");
}
break;
case 2:
{
NSLog(@"333");
}
break;
default:
break;
}
break;
}
}
@end
******************************************************************************
效果图如下: