IOS 扩大UIButton 响应面积
2018-06-08 本文已影响164人
奋斗吧_程序猿
UIButton+EnlargeTouchArea.h 文件
//
// UIButton+EnlargeTouchArea.h
// BtnClickDemo
//
// Created by apple on 2018/6/8.
// Copyright © 2018年 wxp. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIButton (EnlargeTouchArea)
- (void)setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left;
@end
//
// UIButton+EnlargeTouchArea.m
// BtnClickDemo
//
// Created by apple on 2018/6/8.
// Copyright © 2018年 wxp. All rights reserved.
//
#import "UIButton+EnlargeTouchArea.h"
#import <objc/runtime.h>
@implementation UIButton (EnlargeTouchArea)
static char topNameKey;
static char rightNameKey;
static char bottomNameKey;
static char leftNameKey;
/**
* 向按钮的四个方向延伸响应面积
*
* @param top 上间距
* @param left 左间距
* @param bottom 下间距
* @param right 右间距
*/
- (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);
}
//获取当前的响应rect
- (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;
}
}
//系统方法重载
- (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
viewController.m
//
// ViewController.m
// BtnClickDemo
//
// Created by apple on 2018/6/8.
// Copyright © 2018年 wxp. All rights reserved.
//
#import "ViewController.h"
#import "UIButton+EnlargeTouchArea.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.button setEnlargeEdgeWithTop:60 right:80 bottom:100 left:100];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnClick:(id)sender {
NSLog(@"点击");
}
@end
objc_setAssociatedObject 关联对象
objc_setAssociatedObject 相当于 setValue:forKey 进行关联value对象;
- key:要保证全局唯一,key与关联的对象是一一对应关系。必须全局唯一。通常用@selector(methodName)作为key。
- value:要关联的对象。
- policy:关联策略。有五种关联策略。
- OBJC_ASSOCIATION_ASSIGN 等价于 @property(assign)。
- OBJC_ASSOCIATION_RETAIN_NONATOMIC等价于 @property(strong, nonatomic)。
- OBJC_ASSOCIATION_COPY_NONATOMIC等价于@property(copy, nonatomic)。
- OBJC_ASSOCIATION_RETAIN等价于@property(strong,atomic)。
- OBJC_ASSOCIATION_COPY等价于@property(copy, atomic)。