工欲善其事,必先利其器系列之 UIButton+Category

UIButton 按钮点击常用效果,本文介绍:

1、BtnClickBlock 点击Block
2、使用颜色设置按钮背景 - (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state;
3、设置按钮额外热区
4、倒计时按钮
5、点赞动画💗


NO1 BtnClickBlock

1.1 代码

#import <objc/runtime.h>
static const void *UIButtonBlockKey = &UIButtonBlockKey;
@implementation UIButton (Category)

#pragma mark  按钮点击Block

-(void)add_BtnClickHandler:(BtnClickBlock)clickHandlerr{
    objc_setAssociatedObject(self, UIButtonBlockKey, clickHandlerr, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(actionTouched:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)actionTouched:(UIButton *)btn{
    BtnClickBlock block = objc_getAssociatedObject(self, UIButtonBlockKey);
    if (block) {
        block(btn.tag);
    }
}

1.2 使用

  [button04 add_BtnClickHandler:^(NSInteger tag) {
   // do something
    }];

NO2 使用颜色设置按钮背景

2.1 代码

/**
 *  @brief  使用颜色设置按钮背景
 *
 *  @param backgroundColor 背景颜色
 *  @param state           按钮状态
 */

- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state
{
    [self setBackgroundImage:[UIButton imageWithColor:backgroundColor] forState:state];
}
//根据颜色生成一个图片
+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

2.2 使用

  [button01 setBackgroundColor:[UIColor orangeColor] forState:UIControlStateNormal];

NO3 设置按钮额外热区,增加按钮响应范围

3.1 代码

#pragma mark  设置按钮额外热区
/**
 *  @brief  设置按钮额外热区
 */
- (UIEdgeInsets)touchAreaInsets
{
    return [objc_getAssociatedObject(self, @selector(touchAreaInsets)) UIEdgeInsetsValue];
}

- (void)setTouchAreaInsets:(UIEdgeInsets)touchAreaInsets
{
    NSValue *value = [NSValue valueWithUIEdgeInsets:touchAreaInsets];
    objc_setAssociatedObject(self, @selector(touchAreaInsets), value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    UIEdgeInsets touchAreaInsets = self.touchAreaInsets;
    CGRect bounds = self.bounds;
    bounds = CGRectMake(bounds.origin.x - touchAreaInsets.left,
                        bounds.origin.y - touchAreaInsets.top,
                        bounds.size.width + touchAreaInsets.left + touchAreaInsets.right,
                        bounds.size.height + touchAreaInsets.top + touchAreaInsets.bottom);
    return CGRectContainsPoint(bounds, point);
}

3.2 使用

button03.touchAreaInsets=UIEdgeInsetsMake(30, 30, 30, 30);

3.3 效果示例

1B9934C7-4806-48D6-B044-7D2DFCDAB326.jpeg

NO4 倒计时按钮

4.1 代码

#pragma mark  倒计时按钮
/**
 *  @brief  倒计时按钮
 *
 *  @param timeout 时间
 *  @param tittle           tittle
 *  @param waitTittle           waitTittle
 */
-(void)add_startTime:(NSInteger )timeout title:(NSString *)tittle waitTittle:(NSString *)waitTittle{
        __block NSInteger timeOut=timeout; //倒计时时间
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
        dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
        dispatch_source_set_event_handler(_timer, ^{
            if(timeOut<=0){ //倒计时结束,关闭
                dispatch_source_cancel(_timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    //设置界面的按钮显示 根据自己需求设置
                    [self setTitle:tittle forState:UIControlStateNormal];
                    self.userInteractionEnabled = YES;
                });
            }else{
                //            int minutes = timeout / 60;
                int seconds = timeOut % 60;
                NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
                dispatch_async(dispatch_get_main_queue(), ^{
                    //设置界面的按钮显示 根据自己需求设置
                    NSLog(@"____%@",strTime);
                    [self setTitle:[NSString stringWithFormat:@"%@%@",strTime,waitTittle] forState:UIControlStateNormal];
                    self.userInteractionEnabled = NO;
                    
                });
                timeOut--;
                
            }
        });
        dispatch_resume(_timer);
    
}


4.2 使用

   [button02 add_startTime:12 title:@"倒计时"        waitTittle:@"S"];

4.3 效果

5A996B8C-414A-4AC9-AF4C-54D036FDC099.jpeg

NO4 点赞动画

代码

- (void)start_ZanAnimation
{
    CAKeyframeAnimation *clickAnima = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    clickAnima.values = @[@(0.1),@(1.0),@(1.5)];
    clickAnima.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
    clickAnima.calculationMode = kCAAnimationLinear;
    [self.layer addAnimation:clickAnima forKey:@"commendAnimation"];
}

项目传送门
项目地址
https://github.com/SmileMee/SmileHelper.git


我是写代码的凡,如有错误,欢迎指正!🙂🙂🙂

推荐阅读更多精彩内容