美文网首页
UIButton扩大触发热区

UIButton扩大触发热区

作者: 艾希_可可 | 来源:发表于2019-02-26 17:31 被阅读0次

    1、创建category->Object-file->继承UIButton->写文件名
    2

    @interface UIButton (UIButton_EnlargeTouchArea)
    - (void)setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left;
    
    - (void)setEnlargeEdge:(CGFloat) size;
    @end
    
    

    3

    #import "UIButton+UIButton_EnlargeTouchArea.h"
    #import <objc/runtime.h>
    
    @implementation UIButton (UIButton_EnlargeTouchArea)
    static char topNameKey;
    static char rightNameKey;
    static char bottomNameKey;
    static char leftNameKey;
    //点击热区周围扩大,两种方式相同效果,动态赋值
    - (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);
    }
    //点击热区周围扩大
    - (void)setEnlargeEdge:(CGFloat) size{
        objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
        objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
        objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
        objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    
    - (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
    

    4使用

        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 80)];
        button.backgroundColor = [UIColor redColor];
        [self.view addSubview:button];
        [button addTarget:self action:@selector(testButton) forControlEvents:UIControlEventTouchUpInside];
        [button setEnlargeEdge:80];
    //或者,两个方法相同
    //    [button setEnlargeEdgeWithTop:20 right:20 bottom:20 left:50];
    

    相关文章

      网友评论

          本文标题:UIButton扩大触发热区

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