封装顶部选择按钮(用block实现在当前控制器下面添加button点击方法)

来源:互联网 发布:手机荧光灯软件 编辑:程序博客网 时间:2024/06/05 19:58
******************************************************************************
baseView.h
#import<UIKit/UIKit.h>

typedefvoid(^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 = [superinitWithFrame:frame];
   
if (self) {
       
self.titleArr = titleArr;
        [
self_initViews];
    }
   
returnself;
}

-(
void)_initViews
{
   
for (int i = 0; i <_titleArr.count ; i ++) {
       
self.btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
       
_btn.frame = CGRectMake(kScreenWidth/_titleArr.count * i, 0,kScreenWidth/_titleArr.count,self.height - 1);
       
_btn.tag = i;
        [
_btnsetTitle:_titleArr[i]forState:UIControlStateNormal];
        [
_btnsetTitleColor:[UIColorcolorWithRed:0.66fgreen:0.66fblue:0.66falpha:1.00f]forState:UIControlStateNormal];
        [
_btnaddTarget:selfaction:@selector(btnAction:)forControlEvents:UIControlEventTouchUpInside];
        [
selfaddSubview:_btn];
       
    }
   
_smallLineV = [[UIViewalloc]initWithFrame:CGRectMake(0,self.height , kScreenWidth/_titleArr.count,1)];
   
_smallLineV.backgroundColor = [UIColorcolorWithRed:0.24fgreen:0.25fblue:0.23falpha:1.00f];
    [
selfaddSubview:_smallLineV];
   
UIView *lineV = [[UIViewalloc]initWithFrame:CGRectMake(0,0,kScreenWidth,1)];
    lineV.
top =self.height - 1;
    lineV.
backgroundColor = [UIColorcolorWithRed:0.95fgreen:0.95fblue:0.95falpha:1.00f];
    [
selfaddSubview:lineV];

   
}
- (
void)addBlock:(ClickBlock)block
{
   [
_btnaddTarget:selfaction:@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 [selfsubviews])
    {
       
if([ccisKindOfClass:[UIButtonclass]])
        {
           
UIButton *btn = (UIButton *)cc;
            [btn
setTitleColor:[UIColorcolorWithRed:0.66fgreen:0.66fblue:0.66falpha:1.00f]forState:UIControlStateNormal];
        }
    }
   
isSelected = !isSelected;
   
if (!isSelected) {
        [btn
setTitleColor:[UIColorcolorWithRed:0.66fgreen:0.66fblue:0.66falpha:1.00f]forState:UIControlStateNormal];
    }
else
    {
        [btn
setTitleColor:[UIColorcolorWithRed:0.00fgreen:0.00fblue:0.00falpha:1.00fforState: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

@interfaceViewController ()

@end

@implementation ViewController

- (
void)viewDidLoad {
    [
superviewDidLoad];
   
NSArray *arr =@[@"你好",@"是的",@"不是的"];
   
__blockViewController *this =self;
   
baseView *baseV = [[baseViewalloc]initWithFrame:CGRectMake(0,44,kScreenWidth,30)titleArr:arr];
    [baseV
addBlock:^(UIButton *btn) {
        [this
text:btn];
    }];
    [
self.viewaddSubview:baseV];
   
// Do any additional setup after loading the view, typically from a nib.
}

-(
void)text : (UIButton*)btn
{
   
//点击按钮执行的方法
   
switch (btn.tag) {
       
case0:
        {
           
NSLog(@"1111");
        }
           
break;
       
case1:
        {
           
NSLog(@"2222");
 
        }
           
break;
       
case2:
        {
           
NSLog(@"333");

        }
           break;          
       default:
           
break;
    }
}
@end
******************************************************************************
效果图如下:

0 0
原创粉丝点击