iOS中自定義Button按鈕

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)注明出處。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容