Skip to content

Instantly share code, notes, and snippets.

@devxoul
Created March 21, 2013 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devxoul/5215178 to your computer and use it in GitHub Desktop.
Save devxoul/5215178 to your computer and use it in GitHub Desktop.
/*
* UIButton+TouchAreaInsets.h
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2013 Joyfl
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*
*/
#import <UIKit/UIKit.h>
@interface UIButton (TouchAreaInsets)
@property (nonatomic, assign) UIEdgeInsets touchAreaInsets;
@end
/*
* UIButton+TouchAreaInsets.m
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2013 Joyfl
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*
*/
#import "UIButton+TouchAreaInsets.h"
#import <objc/runtime.h>
@implementation UIButton (TouchAreaInsets)
- (UIEdgeInsets)touchAreaInsets
{
return [objc_getAssociatedObject( self, "_touchAreaInsets" ) UIEdgeInsetsValue];
}
- (void)setTouchAreaInsets:(UIEdgeInsets)touchAreaInsets
{
objc_setAssociatedObject( self, "_touchAreaInsets", [NSValue valueWithUIEdgeInsets:touchAreaInsets], 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment