扩大Button按钮的点击区域
2017-05-13 18:11
330 查看
//
// UIButton+RM.h
// ProjectManageEx
//
// Created by mac on 16/7/31.
// Copyright © 2016年 zhangjian01. All rights
reserved.
//
#import
@interface UIButton (RM)
@property (nonatomic, assign)CGFloat enlargedEdge;
- (void)setEnlargedEdgeWithTop:(CGFloat)top left:(CGFloat)left
bottom:(CGFloat)bottom right:(CGFloat)right;
@end
//
// UIButton+RM.m
// ProjectManageEx
//
// Created by mac on 16/7/31.
// Copyright © 2016年 zhangjian01. All rights
reserved.
//
#import "UIButton+RM.h"
@implementation UIButton (RM)
static char topEdgeKey;
static char leftEdgeKey;
static char bottomEdgeKey;
static char rightEdgeKey;
- (void)setEnlargedEdge:(CGFloat)enlargedEdge{
[self
setEnlargedEdgeWithTop:enlargedEdge left:enlargedEdge
bottom:enlargedEdge right:enlargedEdge];
}
- (CGFloat)enlargedEdge{
return
[(NSNumber *)objc_getAssociatedObject(self,
&topEdgeKey)floatValue];
}
- (void)setEnlargedEdgeWithTop:(CGFloat)top left:(CGFloat)left
bottom:(CGFloat)bottom right:(CGFloat)right{
objc_setAssociatedObject(self, &topEdgeKey, [NSNumber
numberWithFloat:top], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &leftEdgeKey, [NSNumber
numberWithFloat:left], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &bottomEdgeKey, [NSNumber
numberWithFloat:bottom], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &rightEdgeKey, [NSNumber
numberWithFloat:right], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGRect)enlargedRect{
NSNumber
*topEdge = objc_getAssociatedObject(self, &topEdgeKey);
NSNumber
*leftEdge = objc_getAssociatedObject(self, &leftEdgeKey);
NSNumber
*bottomEdge = objc_getAssociatedObject(self,
&bottomEdgeKey);
NSNumber
*rightEdge = objc_getAssociatedObject(self,
&rightEdgeKey);
if (topEdge
&& leftEdge && bottomEdge && rightEdge)
{
CGRect enlargedRect = CGRectMake(self.bounds.origin.x -
leftEdge.floatValue, self.bounds.origin.y - topEdge.floatValue,
self.width + leftEdge.floatValue + rightEdge.floatValue,
self.height + topEdge.floatValue + bottomEdge.floatValue);
return enlargedRect;
}else{
return self.bounds;
}
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent
*)event{
if
(self.alpha <= 0.01 || !self.userInteractionEnabled ||
self.hidden) {
return nil;
}
CGRect
enlargedRect = [self enlargedRect];
return
CGRectContainsPoint(enlargedRect, point) ? self : nil;
}
@end
// UIButton+RM.h
// ProjectManageEx
//
// Created by mac on 16/7/31.
// Copyright © 2016年 zhangjian01. All rights
reserved.
//
#import
@interface UIButton (RM)
@property (nonatomic, assign)CGFloat enlargedEdge;
- (void)setEnlargedEdgeWithTop:(CGFloat)top left:(CGFloat)left
bottom:(CGFloat)bottom right:(CGFloat)right;
@end
//
// UIButton+RM.m
// ProjectManageEx
//
// Created by mac on 16/7/31.
// Copyright © 2016年 zhangjian01. All rights
reserved.
//
#import "UIButton+RM.h"
@implementation UIButton (RM)
static char topEdgeKey;
static char leftEdgeKey;
static char bottomEdgeKey;
static char rightEdgeKey;
- (void)setEnlargedEdge:(CGFloat)enlargedEdge{
[self
setEnlargedEdgeWithTop:enlargedEdge left:enlargedEdge
bottom:enlargedEdge right:enlargedEdge];
}
- (CGFloat)enlargedEdge{
return
[(NSNumber *)objc_getAssociatedObject(self,
&topEdgeKey)floatValue];
}
- (void)setEnlargedEdgeWithTop:(CGFloat)top left:(CGFloat)left
bottom:(CGFloat)bottom right:(CGFloat)right{
objc_setAssociatedObject(self, &topEdgeKey, [NSNumber
numberWithFloat:top], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &leftEdgeKey, [NSNumber
numberWithFloat:left], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &bottomEdgeKey, [NSNumber
numberWithFloat:bottom], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &rightEdgeKey, [NSNumber
numberWithFloat:right], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGRect)enlargedRect{
NSNumber
*topEdge = objc_getAssociatedObject(self, &topEdgeKey);
NSNumber
*leftEdge = objc_getAssociatedObject(self, &leftEdgeKey);
NSNumber
*bottomEdge = objc_getAssociatedObject(self,
&bottomEdgeKey);
NSNumber
*rightEdge = objc_getAssociatedObject(self,
&rightEdgeKey);
if (topEdge
&& leftEdge && bottomEdge && rightEdge)
{
CGRect enlargedRect = CGRectMake(self.bounds.origin.x -
leftEdge.floatValue, self.bounds.origin.y - topEdge.floatValue,
self.width + leftEdge.floatValue + rightEdge.floatValue,
self.height + topEdge.floatValue + bottomEdge.floatValue);
return enlargedRect;
}else{
return self.bounds;
}
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent
*)event{
if
(self.alpha <= 0.01 || !self.userInteractionEnabled ||
self.hidden) {
return nil;
}
CGRect
enlargedRect = [self enlargedRect];
return
CGRectContainsPoint(enlargedRect, point) ? self : nil;
}
@end
相关文章推荐
- unity ui button ugui 按钮如何设置点击区域为圆形?【整理转载】
- Android如何扩大按钮点击区域
- IOS返回按钮点击区域扩大的问题
- backBarButtonItem 只要箭头不要文本,且点击空白区域不会触发按钮(强迫症后退按钮)
- 扩大Button的点击区域
- 自定义导航栏的返回按钮,扩大点击区域,不改变图片的尺寸比例,同时可设置title和image的位置关系
- iOS扩大UIButton按钮的可点击区域
- iOS扩大UIButton按钮的可点击区域
- Button按钮及其点击事件方式
- 扩大checkbox的点击区域
- navigationItem 上的按钮 点击按钮区域以外的地方仍会响应点击事件
- Android Listview中Button按钮点击事件冲突解决办法
- iOS UIButton扩大按钮响应区域的解决方法
- andoid点击按钮(ImageButton)时改变按钮的背景图片 SelectorDemo
- 写前端页面时,按钮明明是<button>,为什么点击时会提交表单,刷新页面呢?
- button按钮的点击状态
- iOS 增加UIButton按钮的可点击区域
- javascript实现点击按钮复制指定区域文本
- HTML中jquery控制button按钮可点击、不可点击
- [转载] #CheckBox expand its click area ##CheckBox扩大点击区域