有时候按钮比较小, 不容易点击, 下面说一种扩大按钮点击范围. 方法有很多,这里只说一种
#import <UIKit/UIKit.h> @interface UIButton (TouchAreaInset) /** * @brief 设置按钮额外热区 */ @property (nonatomic, assign) UIEdgeInsets touchAreaInsets; @end
#import <objc/runtime.h> #import "UIButton+TouchAreaInsets.h" @implementation UIButton (TouchAreaInsets) - (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
Demo不上了.