自定义按钮,扩大点击范围
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MyButton : UIButton
@end
NS_ASSUME_NONNULL_END
#import "MyButton.h"
@implementation MyButton
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGRect touchRect = CGRectInset(self.bounds, -20, -20);
if (CGRectContainsPoint(touchRect, point)) {
for (UIView *subView in [self.subviews reverseObjectEnumerator]) {
CGPoint convertedPoint = [subView convertPoint:point fromView:self];
UIView *hitTestView = [subView hitTest:convertedPoint withEvent:event];
if(hitTestView) {
return hitTestView;
}
}
return self;
}
return nil;
}
@end