封装顶部选择按钮(用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;
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.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 = [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.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
@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");
}
#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;
}
break;
}
}
@end
******************************************************************************
效果图如下:
0 0
- 封装顶部选择按钮(用block实现在当前控制器下面添加button点击方法)
- 封装顶部选择按钮(使用block可以在当前控制器下面添加方法)
- iOS开发封装带有Button的UIView控件,使用block给button添加点击事件
- 关于在自定义View中添加Button按钮以及实现点击事件
- 自定义cell创建 button, 在控制器实现点击事件
- listview 下面添加button方法
- 怎么用block给Button和AlertView添加点击事件
- 如何在GWT实现点击Button(或其他Widget)打开文件选择对话框
- 我想在datagrid中选择若干记录,再点击添加按钮,给别一个ACCESS表添加选择的记录,在VB中如何实现?
- 在网页中添加 “返回顶部”按钮
- Android在顶部标题栏添加按钮
- 点击Button按钮,解析在ListView上
- iOS 在tableView上添加button导致按钮没有点击效果和不能滑动的问题
- jquery实现button按钮的点击事件
- Extjs4 grid 行添加按钮(button)的方法
- iOS,OC给button添加block点击事件扩展
- 在scroll下面固定Button的方法
- 实现点击实体菜单按钮显示选项菜单在actionbar 下面的思想来源
- 大数据的几个方面
- 基于文件页的 LRU Cache:磁盘缓存实现
- 【Java】IO字节流
- Module.h 网狐的“com”工具箱
- J2EE之JDBC
- 封装顶部选择按钮(用block实现在当前控制器下面添加button点击方法)
- Tween算法及缓动效果
- html学习笔记
- leetcode 总结,记录,备忘。153
- 大厅的列表服务预览
- LeetCode Word Search
- OCSP server in Windows2012R2 详细搭建过程
- 最常用的字
- oracle 2