From cebc7aea2fc36bfc017b05e75795c129b1ab1a48 Mon Sep 17 00:00:00 2001 From: Sashke Date: Thu, 21 Aug 2014 14:16:47 +0600 Subject: [PATCH] add hitTestEdgeInsets property for making the hit area larger than the default hit area --- MRoundedButton.h | 1 + MRoundedButton.m | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/MRoundedButton.h b/MRoundedButton.h index 2dccb4b..681af51 100644 --- a/MRoundedButton.h +++ b/MRoundedButton.h @@ -49,6 +49,7 @@ extern CGFloat const MRoundedButtonMaxValue; @property (nonatomic, weak) UILabel *detailTextLabel; @property (nonatomic, weak) UIImageView *imageView; @property (nonatomic, assign) UIEdgeInsets contentEdgeInsets; +@property (nonatomic, assign) UIEdgeInsets hitTestEdgeInsets; + (instancetype)buttonWithFrame:(CGRect)frame buttonStyle:(MRoundedButtonStyle)style diff --git a/MRoundedButton.m b/MRoundedButton.m index 1906504..a6deb0f 100644 --- a/MRoundedButton.m +++ b/MRoundedButton.m @@ -132,6 +132,7 @@ - (instancetype)initWithFrame:(CGRect)frame _cornerRadius = 0.0; _borderWidth = 0.0; _contentEdgeInsets = UIEdgeInsetsZero; + _hitTestEdgeInsets = UIEdgeInsetsZero; self.foregroundView = [[UIView alloc] initWithFrame:CGRectNull]; self.foregroundView.backgroundColor = self.foregroundColor; @@ -401,6 +402,18 @@ - (void)fadeOutAnimation } #pragma mark - Touchs + +- (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); + + return CGRectContainsPoint(hitFrame, point); +} + - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *touchView = [super hitTest:point withEvent:event];