UIButton

作者: 夜雨聲煩_ | 来源:发表于2021-04-09 10:02 被阅读0次

    额外热区

    #import <UIKit/UIKit.h>
    
    
    @interface UIButton (JLUtils)
    /**
     *  @brief  设置按钮额外热区
     */
    @property (nonatomic, assign) UIEdgeInsets touchAreaInsets;
    
    @end
    
    #import <objc/runtime.h>
    #import "UIButton+TouchAreaInsets.h"
    
    
    @implementation UIButton (JLUtils)
    
    - (UIEdgeInsets)touchAreaInsets
    {
        return [objc_getAssociatedObject(self, @selector(touchAreaInsets)) UIEdgeInsetsValue];
    }
    /**
     *  @brief  设置按钮额外热区
     */
    - (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);
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:UIButton

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