额外热区
#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
网友评论