您的位置:首页 > 产品设计 > UI/UE

iOS 分类之UIButton+EnlargeEdge

2016-06-27 20:29 567 查看
按钮过小以致无法精确点击,这时我们希望扩大按钮四周或者某一方向来实现按钮的灵敏响应



这里我们创建一个UIButton的分类UIButton(EnlargeEdge)

UIButton+EnlargeEdge.h

//
//  UIButton+EnlargeEdge.h
//  EnlargeButtonEdge
//
//  Created by 黄健 on 16/6/27.
//  Copyright © 2016年 黄健. All rights reserved.
//

#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
//
//  Created by 黄健 on 16/6/27.
//  Copyright © 2016年 黄健. All rights reserved.
//

#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上添加如图所示视图



测试代码

//
//  ViewController.m
//  EnlargeButtonEdge
//
//  Created by 黄健 on 16/6/27.
//  Copyright © 2016年 黄健. All rights reserved.
//

#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


》》运行效果



参考文章

扩大按钮(UIButton)点击范围(随意方向扩展哦)

如何自定义button的点击有效区域
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios uibutton