Files
yuyin_ios/SweetParty/第三方库/【控件扩展】Category/UIButton+Block.m

336 lines
12 KiB
Mathematica
Raw Normal View History

2025-08-08 11:05:33 +08:00
//
// UIButton+Block.m
// ZYPZPro
//
// Created by SystemOuter on 15/11/11.
// Copyright © 2015 SystemOuter. All rights reserved.
//
#import "UIButton+Block.h"
#import <objc/runtime.h>
/**
*
*/
static NSString * const kButtonClicekdBlock = @"kButtonClicekdBlock_PZPro";
/**
*
*/
static NSString * const kButtonMutabelClicekdBlock = @"kButtonMutabelClicekdBlock_PZPro";
/**
*
*/
static NSString * const kButtonControlEventArr = @"kButtonControlEventArr_PZPro";
/**
* Obj-Cmethodself_cmdC
*
* @param self copy
* @param _cmd
*/
void buttonClicked(id self, SEL _cmd){
//
NSString * str_Number = [[NSStringFromSelector(_cmd) componentsSeparatedByString:@"_"] lastObject];
//ControlEvents
NSString * str_Events = [str_Number substringWithRange:NSMakeRange(0, str_Number.length - 1)];
//Block
ZYPZButtonMutabelClickedBlock clickedBlock = objc_getAssociatedObject(self, &kButtonMutabelClicekdBlock);
//
if (clickedBlock) {
// ControlEvents
clickedBlock(self,[str_Events integerValue]);
}
}
@implementation UIButton (Block)
- (void)animationl {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
animation.values = @[@1.4, @1.0];
animation.duration = 0.7;
animation.calculationMode = kCAAnimationCubic;
[self.layer addAnimation:animation forKey:@"transform.scale"];
}
#pragma mark-
/**
* buttonBlock
*
* @param buttonType
* @param basicSet button
*
* @return
*/
+(UIButton *)button_AllocWithType:(UIButtonType)buttonType basicSet:(ZYPZButtonBasicSetBlock)basicSet
{
//
UIButton * btn = [UIButton buttonWithType:buttonType];
//
if (basicSet) {
basicSet(btn);
}
//
return btn;
}
/**
* buttonBlock
*
* @param buttonType
* @param basicSet button
* @param superView
*
* @return
*/
+(UIButton *)button_AllocWithType:(UIButtonType)buttonType basicSet:(ZYPZButtonBasicSetBlock)basicSet addView:(UIView *)superView
{
//
UIButton * btn = [UIButton buttonWithType:buttonType];
//
if (basicSet) {
basicSet(btn);
}
//
[superView addSubview:btn];
//
return btn;
}
/**
*
*
* @param basicSet button
*/
-(void)button_BasicSet:(ZYPZButtonBasicSetBlock)basicSet
{
//
if (basicSet) {
basicSet(self);
}
}
/**
* buttonBlock
*
* @param buttonType
* @param basicSet button
* @param controlEvents
* @param clickedBlock (button)
* @param superView
*
* @return
*/
+(UIButton *)button_AllocWithType:(UIButtonType)buttonType basicSet:(ZYPZButtonBasicSetBlock)basicSet withControlEvents:(UIControlEvents)controlEvents withClicked:(ZYPZButtonClickedBlock)clickedBlock addView:(UIView *)superView
{
//
UIButton * btn = [UIButton buttonWithType:buttonType];
//
if (basicSet) {
basicSet(btn);
}
if (clickedBlock) {
//
objc_setAssociatedObject(btn, &kButtonClicekdBlock, clickedBlock, OBJC_ASSOCIATION_COPY);
}
//
[btn addTarget:btn action:@selector(clickBtn:) forControlEvents:controlEvents];
//
[superView addSubview:btn];
//
return btn;
}
/**
* buttonBlock
*
* @param buttonType
* @param basicSet button
* @param controlEvents
* @param clickedBlock (button)
*
* @return
*/
+(UIButton *)button_AllocWithType:(UIButtonType)buttonType basicSet:(ZYPZButtonBasicSetBlock)basicSet withControlEvents:(UIControlEvents)controlEvents withClicked:(ZYPZButtonClickedBlock)clickedBlock
{
//
UIButton * btn = [UIButton buttonWithType:buttonType];
//
if (basicSet) {
basicSet(btn);
}
if (clickedBlock) {
//
objc_setAssociatedObject(btn, &kButtonClicekdBlock, clickedBlock, OBJC_ASSOCIATION_COPY);
}
//
[btn addTarget:btn action:@selector(clickBtn:) forControlEvents:controlEvents];
//
return btn;
}
/**
* button
*
* @param clickedBlock
* @param controlEvents
*/
-(void)buttonAddTaget:(ZYPZButtonClickedBlock)clickedBlock forControlEvents:(UIControlEvents)controlEvents
{
if (clickedBlock) {
//
objc_setAssociatedObject(self, &kButtonClicekdBlock, clickedBlock, OBJC_ASSOCIATION_COPY);
}
[self addTarget:self action:@selector(clickBtn:) forControlEvents:controlEvents];
}
/**
* buttonBlock
*
* @param buttonType
* @param basicSet button
* @param clickedBlock
* @param controlEvents <UIControlEventsNSNumber>
* @param superView
*
* @return
*/
+(UIButton *)button_AllocWithMutabelType:(UIButtonType)buttonType basicSet:(ZYPZButtonBasicSetBlock)basicSet addMutabelTaget:(ZYPZButtonMutabelClickedBlock)clickedBlock forControlEvents:(NSArray<NSNumber *>*)controlEvents addView:(UIView *)superView
{
//
UIButton * btn = [UIButton buttonWithType:buttonType];
//
if (basicSet) {
basicSet(btn);
}
if (clickedBlock) {
//
objc_setAssociatedObject(self, &kButtonMutabelClicekdBlock, clickedBlock, OBJC_ASSOCIATION_COPY);
//
for (NSNumber * numberEvents in controlEvents) {
//
class_addMethod([UIButton class], NSSelectorFromString([NSString stringWithFormat:@"buttonClicked_%ld:",(long)[numberEvents integerValue]]), (IMP)buttonClicked, "v@:@");
//
[btn addTarget:btn action:NSSelectorFromString([NSString stringWithFormat:@"buttonClicked_%ld:",(long)[numberEvents integerValue]]) forControlEvents:[numberEvents integerValue]];
}
}
//
[superView addSubview:btn];
//
return btn;
}
/**
* buttonBlock
*
* @param buttonType
* @param basicSet button
* @param clickedBlock
* @param controlEvents <UIControlEventsNSNumber>
*
* @return
*/
+(UIButton *)button_AllocWithMutabelType:(UIButtonType)buttonType basicSet:(ZYPZButtonBasicSetBlock)basicSet addMutabelTaget:(ZYPZButtonMutabelClickedBlock)clickedBlock forControlEvents:(NSArray<NSNumber *>*)controlEvents
{
//
UIButton * btn = [UIButton buttonWithType:buttonType];
//
if (basicSet) {
basicSet(btn);
}
if (clickedBlock) {
//
objc_setAssociatedObject(self, &kButtonMutabelClicekdBlock, clickedBlock, OBJC_ASSOCIATION_COPY);
//
for (NSNumber * numberEvents in controlEvents) {
//
class_addMethod([UIButton class], NSSelectorFromString([NSString stringWithFormat:@"buttonClicked_%ld:",(long)[numberEvents integerValue]]), (IMP)buttonClicked, "v@:@");
//
[btn addTarget:btn action:NSSelectorFromString([NSString stringWithFormat:@"buttonClicked_%ld:",(long)[numberEvents integerValue]]) forControlEvents:[numberEvents integerValue]];
}
}
//
return btn;
}
/**
* button
*
* @param clickedBlock
* @param controlEvents <UIControlEventsNSNumber>
*/
-(void)buttonAddMutabelTaget:(ZYPZButtonMutabelClickedBlock)clickedBlock forControlEvents:(NSArray<NSNumber *>*)controlEvents
{
if (clickedBlock) {
//
objc_setAssociatedObject(self, &kButtonMutabelClicekdBlock, clickedBlock, OBJC_ASSOCIATION_COPY);
//
for (NSNumber * numberEvents in controlEvents) {
//
class_addMethod([UIButton class], NSSelectorFromString([NSString stringWithFormat:@"buttonClicked_%ld:",(long)[numberEvents integerValue]]), (IMP)buttonClicked, "v@:@");
//
[self addTarget:self action:NSSelectorFromString([NSString stringWithFormat:@"buttonClicked_%ld:",(long)[numberEvents integerValue]]) forControlEvents:[numberEvents integerValue]];
}
}
}
-(void)setImage:(UIImage *)image withTitle:(NSString *)title forState:(UIControlState)stateType
{
//
CGSize size = CGSizeZero;
//
if ([NSString instancesRespondToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
//IOS7.0
size = [title sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
}
if (title.length <= 0) {
// UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
[self.imageView setContentMode:UIViewContentModeCenter];
[self setImageEdgeInsets:UIEdgeInsetsMake(.0f,.0f,0.0,0.0)];
[self setImage:image forState:stateType];
[self.titleLabel setContentMode:UIViewContentModeCenter];
[self.titleLabel setBackgroundColor:kClearColor];
[self setTitleEdgeInsets:UIEdgeInsetsMake(.0f,.0f,0.0,0.0)];
[self setTitle:title forState:stateType];
}else{
// UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
[self.imageView setContentMode:UIViewContentModeCenter];
[self setImageEdgeInsets:UIEdgeInsetsMake(-size.height + 5.f,0.0,0.0,-size.width)];
[self setImage:image forState:stateType];
[self.titleLabel setContentMode:UIViewContentModeCenter];
[self.titleLabel setBackgroundColor:kClearColor];
// [self setTitleEdgeInsets:(image.size.height + 3.f,-image.size.width,0.0,0.0)];
[self setTitle:title forState:stateType];
}
}
#pragma mark-
//
-(void)clickBtn:(UIButton *)btn
{
ZYPZButtonClickedBlock clickedBlock = objc_getAssociatedObject(btn, &kButtonClicekdBlock);
if (clickedBlock) {
clickedBlock(btn);
}
}
@end