美文网首页iOS高级开发
IOS 扩大UIButton 响应面积

IOS 扩大UIButton 响应面积

作者: 奋斗吧_程序猿 | 来源:发表于2018-06-08 14:25 被阅读164次

    UIButton+EnlargeTouchArea.h 文件

    //
    //  UIButton+EnlargeTouchArea.h
    //  BtnClickDemo
    //
    //  Created by apple on 2018/6/8.
    //  Copyright © 2018年 wxp. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface UIButton (EnlargeTouchArea)
    
    - (void)setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left; 
    @end
    
    
    //
    //  UIButton+EnlargeTouchArea.m
    //  BtnClickDemo
    //
    //  Created by apple on 2018/6/8.
    //  Copyright © 2018年 wxp. All rights reserved.
    //
    
    #import "UIButton+EnlargeTouchArea.h"
    #import <objc/runtime.h>
    @implementation UIButton (EnlargeTouchArea)
    
    static char topNameKey;
    static char rightNameKey;
    static char bottomNameKey;
    static char leftNameKey;
    
    
    /**
     *  向按钮的四个方向延伸响应面积
     *
     *  @param top    上间距
     *  @param left   左间距
     *  @param bottom 下间距
     *  @param right  右间距
     */
    - (void) setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left
    {
        objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
        objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
        objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
        objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    
    //获取当前的响应rect
    - (CGRect) enlargedRect
    {
        NSNumber* topEdge = objc_getAssociatedObject(self, &topNameKey);
        NSNumber* rightEdge = objc_getAssociatedObject(self, &rightNameKey);
        NSNumber* bottomEdge = objc_getAssociatedObject(self, &bottomNameKey);
        NSNumber* leftEdge = objc_getAssociatedObject(self, &leftNameKey);
        if (topEdge && rightEdge && bottomEdge && leftEdge)
        {
            return CGRectMake(self.bounds.origin.x - leftEdge.floatValue, self.bounds.origin.y - topEdge.floatValue, self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue, self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
        }
        else
        {
            return self.bounds;
        }
    }
    
    //系统方法重载
    - (UIView*) hitTest:(CGPoint) point withEvent:(UIEvent*) event
    {
        CGRect rect = [self enlargedRect];
        if (CGRectEqualToRect(rect, self.bounds))
        {
            return [super hitTest:point withEvent:event];
        }
        return CGRectContainsPoint(rect, point) ? self : nil;
    } 
    @end
    
    
    

    viewController.m

    //
    //  ViewController.m
    //  BtnClickDemo
    //
    //  Created by apple on 2018/6/8.
    //  Copyright © 2018年 wxp. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "UIButton+EnlargeTouchArea.h"
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self.button setEnlargeEdgeWithTop:60 right:80 bottom:100 left:100];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (IBAction)btnClick:(id)sender {
        NSLog(@"点击");
    }
    
    @end
    
    objc_setAssociatedObject 关联对象

    objc_setAssociatedObject 相当于 setValue:forKey 进行关联value对象;

    • key:要保证全局唯一,key与关联的对象是一一对应关系。必须全局唯一。通常用@selector(methodName)作为key。
    • value:要关联的对象。
    • policy:关联策略。有五种关联策略。
    • OBJC_ASSOCIATION_ASSIGN 等价于 @property(assign)。
    • OBJC_ASSOCIATION_RETAIN_NONATOMIC等价于 @property(strong, nonatomic)。
    • OBJC_ASSOCIATION_COPY_NONATOMIC等价于@property(copy, nonatomic)。
    • OBJC_ASSOCIATION_RETAIN等价于@property(strong,atomic)。
    • OBJC_ASSOCIATION_COPY等价于@property(copy, atomic)。

    相关文章

      网友评论

      • ideago:不能用呀,大兄弟,你确定你那边能用么?我测试,点击扩展区域压根都不执行hitTest方法的。
        ideago:@奋斗吧_程序猿 下载demo看了下,你的确实没问题呢,我好好看看我的问题在哪吧,我是给navigationItem的leftBarButtonItem里的一个 自定义返回按钮扩大面积的,估计是navigationItem那边有影响
        奋斗吧_程序猿:@非空想家 我这边是没有问题,你这个demo跑看看
        https://github.com/XiaopengWang90/ButtonEnlargeTouchArea

      本文标题:IOS 扩大UIButton 响应面积

      本文链接:https://www.haomeiwen.com/subject/ithasftx.html