美文网首页iOS
iOS 扩大点击区域

iOS 扩大点击区域

作者: yizhaorong | 来源:发表于2017-04-07 15:52 被阅读11次

    UIControl+OMTExtension.h

    //
    //  UIControl+OMTExtension.h
    //  OneMTDemo
    //
    //  Created by yizhaorong on 2017/4/7.
    //  Copyright © 2017年 onemt. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface UIControl (OMTExtension)
    
    @property (nonatomic, assign) UIEdgeInsets hitTestEdgeInsets;
    
    @property (nonatomic, assign) BOOL debugHitTestEdgeInsets;
    
    @end
    
    

    UIControl+OMTExtension.m

    //
    //  UIControl+OMTExtension.m
    //  OneMTDemo
    //
    //  Created by yizhaorong on 2017/4/7.
    //  Copyright © 2017年 onemt. All rights reserved.
    //
    
    #import "UIControl+OMTExtension.h"
    #import <objc/runtime.h>
    
    static NSString *const kHitTestEdgeInsetsKey = @"hitTestEdgeInsets";
    
    static NSString *const kDebugHitTestEdgeInsetsKey = @"debugHitTestEdgeInsets";
    
    static NSString *const kDebugLayerName = @"kDebugLayerName";
    
    @implementation UIControl (OMTExtension)
    
    - (void)setHitTestEdgeInsets:(UIEdgeInsets)hitTestEdgeInsets {
        NSValue *value = [NSValue value:&hitTestEdgeInsets withObjCType:@encode(UIEdgeInsets)];
        objc_setAssociatedObject(self, &kHitTestEdgeInsetsKey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (UIEdgeInsets)hitTestEdgeInsets {
        NSValue *value = objc_getAssociatedObject(self, &kHitTestEdgeInsetsKey);
        if(value) {
            UIEdgeInsets edgeInsets;
            [value getValue:&edgeInsets];
            return edgeInsets;
        }else {
            return UIEdgeInsetsZero;
        }
    }
    
    - (void)setDebugHitTestEdgeInsets:(BOOL)debugHitTestEdgeInsets {
        NSValue *value = [NSValue value:&debugHitTestEdgeInsets withObjCType:@encode(BOOL)];
        objc_setAssociatedObject(self, &kDebugHitTestEdgeInsetsKey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (BOOL)debugHitTestEdgeInsets {
        NSValue *value = objc_getAssociatedObject(self, &kDebugHitTestEdgeInsetsKey);
        if(value) {
            BOOL debugEdgeInsets;
            [value getValue:&debugEdgeInsets];
            return debugEdgeInsets;
        }else {
            return NO;
        }
    }
    
    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
        if(UIEdgeInsetsEqualToEdgeInsets(self.hitTestEdgeInsets, UIEdgeInsetsZero) || !self.enabled || self.hidden) {
            return [super pointInside:point withEvent:event];
        }
        
        CGRect relativeFrame = self.bounds;
        CGRect hitFrame = UIEdgeInsetsInsetRect(relativeFrame, self.hitTestEdgeInsets);
        
        if (self.debugHitTestEdgeInsets) {
            CALayer *layer = [self debugLayer];
            [layer removeFromSuperlayer];
            layer.frame = hitFrame;
            [self.layer insertSublayer:layer atIndex:0];
        }
        return CGRectContainsPoint(hitFrame, point);
    }
    
    - (CALayer *)debugLayer {
        CALayer *layer = nil;
        for (CALayer *subLayer in self.layer.sublayers) {
            if ([subLayer.name isEqualToString:kDebugLayerName]) {
                layer = subLayer;
                break;
            }
        }
        if (layer) return layer;
        layer = [CALayer layer];
        CGFloat red = arc4random_uniform(255) / 255.0;
        CGFloat green = arc4random_uniform(255) / 255.0;
        CGFloat blue = arc4random_uniform(255) / 255.0;
        layer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:0.5].CGColor;
        layer.name = kDebugLayerName;
        return layer;
    }
    
    @end
    
    

    相关文章

      网友评论

        本文标题:iOS 扩大点击区域

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