美文网首页
UIButton扩大点击范围

UIButton扩大点击范围

作者: 否极泰来_L | 来源:发表于2019-02-20 17:53 被阅读3次

    我们在项目开发中可能会出现按钮较小,用户不容易点击的情况。

    解决方法

    新建UIButton分类,重写- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event;改变按钮的有效点击区域

    代码

    UIButton+HLClickRange.h
    typedef struct HLClickEdgeInsets {
        
        CGFloat top,left,bottom,right;
        
    } HLClickEdgeInsets;
    
    UIKIT_STATIC_INLINE HLClickEdgeInsets HLClickEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) {
        
        HLClickEdgeInsets clickEdgeInsets = {top, left, bottom, right};
        return clickEdgeInsets;
    }
    
    @interface UIButton (HLClickRange)
    
    /**
     改变button的点击范围
     length:范围边缘距离(四个边缘同样距离)
     */
    - (void)hlChangeButtonClickLength:(CGFloat)length;
    
    /**
     改变button的点击范围
     edgeInsets:范围边缘距离
     */
    - (void)hlChangeButtonClickRange:(HLClickEdgeInsets)edgeInsets;
    
    @end
    
    UIButton+HLClickRange.m
    #import "UIButton+HLClickRange.h"
    #import <objc/runtime.h>
    
    static char hlTopKey;
    static char hlLeftKey;
    static char hlBottomKey;
    static char hlRightKey;
    
    @implementation UIButton (HLClickRange)
    
    - (void)hlChangeButtonClickLength:(CGFloat)length{
        
        [self hlChangeButtonClickRange:HLClickEdgeInsetsMake(length, length, length, length)];
    }
    
    - (void)hlChangeButtonClickRange:(HLClickEdgeInsets)edgeInsets{
        
        objc_setAssociatedObject(self, &hlTopKey, [NSNumber numberWithFloat:edgeInsets.top], OBJC_ASSOCIATION_COPY_NONATOMIC);
        objc_setAssociatedObject(self, &hlLeftKey, [NSNumber numberWithFloat:edgeInsets.left], OBJC_ASSOCIATION_COPY_NONATOMIC);
        objc_setAssociatedObject(self, &hlBottomKey, [NSNumber numberWithFloat:edgeInsets.bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
        objc_setAssociatedObject(self, &hlRightKey, [NSNumber numberWithFloat:edgeInsets.right], OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    
    - (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;
    }
    
    - (CGRect)enlargedRect
    {
        NSNumber *topEdge = objc_getAssociatedObject(self, &hlTopKey);
        NSNumber *leftEdge = objc_getAssociatedObject(self, &hlLeftKey);
        NSNumber *bottomEdge = objc_getAssociatedObject(self, &hlBottomKey);
        NSNumber *rightEdge = objc_getAssociatedObject(self, &hlRightKey);
       
        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;
        }
    }
    
    @end
    

    Demo地址

    相关文章

      网友评论

          本文标题:UIButton扩大点击范围

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