iOS 分類之UIButton+EnlargeEdge

前提:按鈕過小以致無法精確點擊,這時我們希望擴大按鈕四周或者某一方向來實現(xiàn)按鈕的靈敏響應

這里我們創(chuàng)建一個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

參考文獻:
http://www.itdecent.cn/p/ce2d3191224f

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,067評論 4 61
  • 1.你的公眾號目標用戶是誰? 目標用戶: 愛旅游,關注旅游線路,簽證,生活資訊,攝影,20歲以上,以女性為主的用戶...
    SeekerChen閱讀 301評論 0 0
  • 蔣勛先生說過: 你被孤獨驅趕著去尋找遠離孤獨的方式時,會處于一種非??膳碌臓顟B(tài);因為無法和自己相處的人,也很難和別...
    Chaylse醬閱讀 273評論 0 0
  • 生活是你的,希望過成你想要的樣子。 我們總覺得生活太過于平淡,偶爾給生活來點驚喜,也不是不可以。 心血來潮,就想動...
    佐伊在這里閱讀 370評論 1 3
  • 昨晚看完《含淚活著》的時候已經(jīng)很晚,眼睛哭的很痛,立即就睡了。 今早醒來眼睛也還是不舒服。匆忙補完日記,決定放松一...
    作者伍辰閱讀 451評論 2 2

友情鏈接更多精彩內容