您的位置:首页 > 移动开发 > IOS开发

不改变button大小,扩大点击范围

2017-05-16 15:29 375 查看
如图所示:



假设我们要做类似上图的一个效果,点击关闭图片的时候关闭窗口。但是为了整体UI效果,按钮做的小了一些,这时候会影响点击效果。(诚然,有的时候你觉得按钮做大一点不就行了吗,我也是这么想的,可是产品和UI会用一千个理由怼回来的)现在我贴出一个已经测试过的方法供大家使用。

我们将要实现的效果如图:



1.新建一个UIButton的Category – UIButton+EnlargeEdge

UIButton+EnlargeEdge.h

#import <UIKit/UIKit.h>
#import <objc/runtime.h>

@interface UIButton (EnlargeEdge)

/** 设置可点击范围到按钮边缘的距离 */
- (void)setEnlargeEdge:(CGFloat)size;

/** 设置可点击范围到按钮上、右、下、左的距离 */
- (void)setEnlargeEdgeWithTop:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom left:(CGFloat)left;

@end


UIButton+EnlargeEdge.m

#import "UIButton+EnlargeEdge.h"

static char topNameKey;
static char rightNameKey;
static char bottomNameKey;
static char leftNameKey;

@implementation UIButton (EnlargeEdge)

- (void)setEnlargeEdge:(CGFloat)size {
objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)setEnlargeEdgeWithTop:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom left:(CGFloat)left {
objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:left], 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;
}

@end


2.ViewController.m测试代码

#import "ViewController.h"
#import "UIButton+EnlargeEdge.h"

@interface ViewController ()

@property (nonatomic, strong) UIButton *button1;
@property (nonatomic, strong) UIButton *button2;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
self.button1.frame = CGRectMake(150, 100, self.view.bounds.size.width-300, self.view.bounds.size.width-300);
self.button1.backgroundColor = [UIColor blueColor];
self.button1.tag = 1001;
[self.button1 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.button1 setEnlargeEdge:10];
[self.view addSubview:self.button1];

self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
self.button2.frame = CGRectMake(150, 300, self.view.bounds.size.width-300, self.view.bounds.size.width-300);
self.button2.backgroundColor = [UIColor greenColor];
self.button2.tag = 1002;
[self.button2 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.button2 setEnlargeEdgeWithTop:50 right:50 bottom:200 left:50];
[self.view addSubview:self.button2];
}

- (void)buttonClick:(UIButton *)btn {
NSString *msg = nil;
if (btn.tag == 1001) {
msg = @"点击button1";
} else if (btn.tag == 1002) {
msg = @"点击button2";
}
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:msg preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
}

@end


代码就这么多,就不贴demo了,有需要的直接copy代码就好了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息