Skip to content

Commit

Permalink
Merge pull request #19 from xinsight/increase-touch-area
Browse files Browse the repository at this point in the history
increase touch area if checkbox is smaller than 44pt
  • Loading branch information
Boris-Em committed Apr 14, 2016
2 parents 0632286 + b614b93 commit 257da07
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Classes/BEMCheckBox.h
Expand Up @@ -118,6 +118,10 @@ typedef NS_ENUM(NSInteger, BEMAnimationType) {
*/
@property (nonatomic) BEMAnimationType offAnimationType;

/** If the checkbox width or height is smaller than this value, the touch area will be increased. Allows for visually small checkboxes to still be easily tapped. Default: (44, 44)
*/
@property (assign, nonatomic) IBInspectable CGSize minimumTouchSize;

/** Set the state of the check box to On or Off, optionally animating the transition.
*/
- (void)setOn:(BOOL)on animated:(BOOL)animated;
Expand Down
23 changes: 23 additions & 0 deletions Classes/BEMCheckBox.m
Expand Up @@ -59,6 +59,7 @@ - (void)commonInit {
_tintColor = [UIColor lightGrayColor];
_lineWidth = 2.0;
_animationDuration = 0.5;
_minimumTouchSize = CGSizeMake(44, 44);
_onAnimationType = BEMAnimationTypeStroke;
_offAnimationType = BEMAnimationTypeStroke;
self.backgroundColor = [UIColor clearColor];
Expand Down Expand Up @@ -173,6 +174,28 @@ - (void)handleTapCheckBox:(UITapGestureRecognizer *)recognizer {
}

#pragma mark - Helper methods -

#pragma mark Increase touch area
- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event;
{
BOOL found = [super pointInside:point withEvent:event];

CGSize minimumSize = self.minimumTouchSize;
CGFloat width = self.bounds.size.width;
CGFloat height = self.bounds.size.height;

if (found == NO && (width < minimumSize.width || height < minimumSize.height)) {
CGFloat increaseWidth = minimumSize.width - width;
CGFloat increaseHeight = minimumSize.height - height;

CGRect rect = CGRectInset(self.bounds, (-increaseWidth / 2), (-increaseHeight / 2));

found = CGRectContainsPoint(rect, point);
}

return found;
}

#pragma mark Drawings
- (void)drawRect:(CGRect)rect {
[self setOn:self.on animated:NO];
Expand Down

0 comments on commit 257da07

Please sign in to comment.