iOS開發(fā)中,經(jīng)常用到Button,在此我封裝了一些方法可以快速使用UIButton,一個(gè)方法就可以創(chuàng)建想要效果的button,也可以自己在這個(gè)基礎(chǔ)上再封裝。
一. ZJButtton.h文件中聲明+方法,在@interface ZJButton : UIButton繼承類名上方定義Block,這里不介紹Block的實(shí)現(xiàn)原理,重點(diǎn)介紹用Block實(shí)現(xiàn)自定義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中實(shí)現(xiàn)+方法
在.m文件中聲明一個(gè)傳遞Block
@interface ZJButton()
//注意:聲明block類型使用copy
@property(nonatomic,copy)buttonBlock tempBlock;
@end
.m中實(shí)現(xiàn)+方法
+(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{
//調(diào)用block變量
if (self.tempBlock) {//判斷是否實(shí)現(xiàn)Block
self.tempBlock();
}
}
僅供初學(xué)者學(xué)習(xí)使用,歡迎轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)注明出處。