iOS中自定义Button按钮

作者: iOS俱哥 | 来源:发表于2016-06-30 10:54 被阅读4786次

iOS开发中,经常用到Button,在此我封装了一些方法可以快速使用UIButton,一个方法就可以创建想要效果的button,也可以自己在这个基础上再封装。

一. ZJButtton.h文件中声明+方法,在@interface ZJButton : UIButton继承类名上方定义Block,这里不介绍Block的实现原理,重点介绍用Block实现自定义button

typedef void (^buttonBlock)();//声明Block

+(ZJButton *)buttonWithFrame:(CGRect)frame title:(NSString *)title andBlock:(buttonBlock)myBlock;

+(ZJButton *)buttonWithtitle:(NSString *)title Block:(buttonBlock)block;

+(ZJButton *)buttonWithFrame:(CGRect)frame title:(NSString *)title selectedTitle:(NSString *)selectedTitle andBlock:(buttonBlock)myBlock;

+(ZJButton *)buttonWithFrame:(CGRect)frame imageName:(NSString *)imageName andBlock:(buttonBlock)myBlock;

+(ZJButton *)buttonWithFrame:(CGRect)frame imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName andBlock:(buttonBlock)myBlock;

二.在ZJButton.m中实现+方法

在.m文件中声明一个传递Block

@interface ZJButton()

//注意:声明block类型使用copy

@property(nonatomic,copy)buttonBlock tempBlock;

@end

.m中实现+方法

+(ZJButton *)buttonWithFrame:(CGRect)frame title:(NSString *)title andBlock:(buttonBlock)myBlock{

ZJButton *button=[ZJButton buttonWithType:UIButtonTypeCustom];//自定义

button.frame=frame;

[button setTitle:title forState:UIControlStateNormal];

[button addTarget:button action:@selector(buttonBlockClick:) forControlEvents:UIControlEventTouchUpInside];

[button setBackgroundImage:[UIImage imageNamed:@"buttonbar_action"] forState:UIControlStateNormal];

[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

button.tempBlock=myBlock;

return button;

}

+(ZJButton *)buttonWithtitle:(NSString *)title Block:(buttonBlock)block{

ZJButton *button=[ZJButton buttonWithType:UIButtonTypeCustom];//自定义

button.frame=CGRectMake(0, 0, 0, 0);

[button addTarget:button action:@selector(buttonBlockClick:) forControlEvents:UIControlEventTouchUpInside];

[button setTitle:title forState:UIControlStateNormal];

button.tempBlock=block;

return button;

}

+(ZJButton *)buttonWithFrame:(CGRect)frame title:(NSString *)title selectedTitle:(NSString *)selectedTitle andBlock:(buttonBlock)myBlock{

ZJButton *button=[ZJButton buttonWithType:UIButtonTypeCustom];//自定义

button.frame=frame;

[button setTitle:title forState:UIControlStateNormal];

[button setTitle:selectedTitle forState:UIControlStateSelected];

[button addTarget:button action:@selector(buttonBlockClick:) forControlEvents:UIControlEventTouchUpInside];

[button setBackgroundImage:[UIImage imageNamed:@"buttonbar_action"] forState:UIControlStateNormal];

[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

button.tempBlock=myBlock;

return button;

}

+(ZJButton *)buttonWithFrame:(CGRect)frame imageName:(NSString *)imageName andBlock:(buttonBlock)myBlock{

ZJButton *button=[ZJButton buttonWithType:UIButtonTypeCustom];//自定义

button.frame=frame;

//[button setTitle:title forState:UIControlStateNormal];

[button addTarget:button action:@selector(buttonBlockClick:) forControlEvents:UIControlEventTouchUpInside];

UIImage *image=[UIImage imageNamed:imageName];

// image=[image stretchableImageWithLeftCapWidth:20 topCapHeight:0];//拉

[button setBackgroundImage:image forState:UIControlStateNormal];

[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

button.tempBlock=myBlock;

return button;

}

+(ZJButton *)buttonWithFrame:(CGRect)frame imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName andBlock:(buttonBlock)myBlock{

ZJButton *button = [ZJButton buttonWithFrame:frame imageName:imageName andBlock:myBlock];

[button setBackgroundImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateHighlighted];

[button setBackgroundImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateSelected];

return button;

}

-(void)buttonBlockClick:(ZJButton *)button{

//调用block变量

if (self.tempBlock) {//判断是否实现Block

self.tempBlock();

}

}

仅供初学者学习使用,欢迎转载,转载请注明出处。

相关文章

网友评论

  • 国王or乞丐:LZ有时间可以看下链式编程和响应式编程,这样封装感觉好low,当然我不是说你,因为我现在也是这样封装的,不过目前在改了,我这边使用的是链式编程写的,具体点击这里还没有完全能实现
    艳晓:链式编程的自定义button有Demo吗?最近正在看,求指导。
    国王or乞丐:@iOS俱哥 嗯嗯,主要我之前也是这样写的,老大最近让我更新呢,说是全部让使用链式编程,但是按钮哪里还是没实现,你那边可以实现的话请多多指导:smile:
    iOS俱哥:@国王or乞丐 这个是之前封装的了,不能以现在的水平看待之前的东西,技能总是在提升中,我虚心接受你的建议。等到有时间了我更新下。

本文标题:iOS中自定义Button按钮

本文链接:https://www.haomeiwen.com/subject/lqnxjttx.html