前提:按钮过小以致无法精确点击,这时我们希望扩大按钮四周或者某一方向来实现按钮的灵敏响应
这里我们创建一个UIButton的分类UIButton(EnlargeEdge)
UIButton+EnlargeEdge.h
//
// UIButton+EnlargeEdge.h
// EnlargeButtonEdge
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@interface UIButton (EnlargeEdge)
/**
* @author hj, 06.27 2016 20:06
*
* 同时向按钮的四个方向延伸响应面积
*
* @param size 间距
*/
- (void)setEnlargeEdge:(CGFloat) size;
/**
* @author hj, 06.27 2016 20:06
*
* 向按钮的四个方向延伸响应面积
*
* @param top 上间距
* @param left 左间距
* @param bottom 下间距
* @param right 右间距
*/
- (void)setEnlargeEdgeWithTop:(CGFloat) top left:(CGFloat) left bottom:(CGFloat) bottom right:(CGFloat) right;
@end
UIButton+EnlargeEdge.m
//
// UIButton+EnlargeEdge.m
// EnlargeButtonEdge
#import "UIButton+EnlargeEdge.h"
@implementation UIButton (EnlargeEdge)
static char topNameKey;
static char leftNameKey;
static char bottomNameKey;
static char rightNameKey;
- (void)setEnlargeEdge:(CGFloat) size
{
objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &bottomNameKey,[NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void)setEnlargeEdgeWithTop:(CGFloat) top left:(CGFloat) left bottom:(CGFloat) bottom right:(CGFloat) right
{
objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &bottomNameKey,[NSNumber numberWithFloat:bottom],OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (CGRect)enlargedRect
{
NSNumber* topEdge = objc_getAssociatedObject(self, &topNameKey);
NSNumber* rightEdge = objc_getAssociatedObject(self, &rightNameKey);
NSNumber* bottomEdge = objc_getAssociatedObject(self, &bottomNameKey);
NSNumber* leftEdge = objc_getAssociatedObject(self, &leftNameKey);
if (topEdge && rightEdge && bottomEdge && leftEdge)
{
return CGRectMake(self.bounds.origin.x - leftEdge.floatValue,
self.bounds.origin.y - topEdge.floatValue,
self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue,
self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
} else
{
return self.bounds;
}
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
CGRect rect = [self enlargedRect];
if (CGRectEqualToRect(rect, self.bounds))
{
return [super pointInside:point withEvent:event];
}
return CGRectContainsPoint(rect, point) ? YES : NO;
}
/**
针对UIView及其派生类
- (UIView*)hitTest:(CGPoint) point withEvent:(UIEvent*) event
{
CGRect rect = [self enlargedRect];
if (CGRectEqualToRect(rect, self.bounds))
{
return [super hitTest:point withEvent:event];
}
return CGRectContainsPoint(rect, point) ? self : nil;
}
**/
@end
》》测试
先在Main.storyboard上添加如图所示视图
![Uploading 20160627202848171_568120.png . . .]
测试代码
//
// ViewController.m
// EnlargeButtonEdge
//
#import "ViewController.h"
#import "UIButton+EnlargeEdge.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *button;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.button setEnlargeEdge:20];
// [self.button setEnlargeEdgeWithTop:0 left:0 bottom:0 right:20];
}
- (IBAction)buttonAction:(UIButton *)sender
{
NSLog(@"点我干甚");
}
@end