Custom Button button in iOS

IOS development, often used Button, in which I encapsulate a number of methods can quickly use UIButton, a method can create the desired effect of the button, you can also re packaging on this basis.

ZJButtton.h file declaration + method, in the @interface ZJButton: UIButton class name above the definition of Block, where Block does not introduce the realization of the principle, focusing on the use of Block to achieve custom button

Typedef void (^buttonBlock) (
); / / Block statement

+ (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 * andBlock: (selectedTitle) myBlock; buttonBlock) + (ZJButton *) buttonWithFrame: (CGRect) frame imageName: (NSString *) imageName andBlock: (buttonBlock) myBlock; + (ZJButton *) buttonWithFrame: (CGRect) frame imageName: (NSString * imageName) selectedImageName: (NSString *) selectedImageName andBlock: (buttonBlock) myBlock;

Two. Implement + method in ZJButton.m

Declare an Block in the.M file

@interface ZJButton (
) / / Note: statement of type block using 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; / / custom; 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]; / / custom 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; / / custom; 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; / / custom; forState: UIControlStateNormal]; [button addTarget:button action:@selector (buttonBlockClick:) forControlEvents:UIControlEventTouchUpInside]; UIImage *image=[UIImage imageNamed:imageName]; / / image=[image / / [button setBackgroundImage:image stretchableImageWithLeftCapWidth:20 topCapHeight:0]; [button setTitleColor:[UIColor La forState:UIControlStateNormal]; 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{/ / call the block variable if (self.tempBlock) to determine whether Block self.tempBlock ({//);}}

For beginners to learn to use, welcome to reprint, please indicate the source.