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();
}
}
仅供初学者学习使用,欢迎转载,转载请注明出处。