iOS 分类之UIButton+EnlargeEdge

前提:按钮过小以致无法精确点击,这时我们希望扩大按钮四周或者某一方向来实现按钮的灵敏响应

这里我们创建一个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

参考文献:
//www.greatytc.com/p/ce2d3191224f

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,276评论 4 61
  • 1.你的公众号目标用户是谁? 目标用户: 爱旅游,关注旅游线路,签证,生活资讯,摄影,20岁以上,以女性为主的用户...
    SeekerChen阅读 268评论 0 0
  • 蒋勋先生说过: 你被孤独驱赶着去寻找远离孤独的方式时,会处于一种非常可怕的状态;因为无法和自己相处的人,也很难和别...
    Chaylse酱阅读 251评论 0 0
  • 生活是你的,希望过成你想要的样子。 我们总觉得生活太过于平淡,偶尔给生活来点惊喜,也不是不可以。 心血来潮,就想动...
    佐伊在这里阅读 321评论 1 3
  • 昨晚看完《含泪活着》的时候已经很晚,眼睛哭的很痛,立即就睡了。 今早醒来眼睛也还是不舒服。匆忙补完日记,决定放松一...
    作者伍辰阅读 383评论 2 2