美文网首页
增加UIButton 点击区域

增加UIButton 点击区域

作者: Luyc_Han | 来源:发表于2018-05-15 11:17 被阅读9次
#import <UIKit/UIKit.h>

@interface UIButton (EnlargeTouchArea)

- (void)setEnlargeEdge:(CGFloat)size;

@end
#import "UIButton+EnlargeTouchArea.h"
#import <objc/runtime.h>

@implementation UIButton (EnlargeTouchArea)

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

- (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;
    }
}

- (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

相关文章

  • 增加UIButton 点击区域

  • 增加UIButton点击区域

    在平时开发的时候经常会遇到UI的给的设计图button很小,但是老板就说不好点击,让你凭空增加点击区域的情况,此时...

  • 扩展UIButton的点击区域

    扩展UIButton的点击区域

  • 扩大UIButton点击区域

    当设计图上的给出按钮尺寸较小,我们将对应的资源文件放入UIButton中,比如只有12x12pt,在真机调试中会发...

  • 增加UIButton的响应区域

    经常遇到UIButton对象"点了没反应"或"很难被点击到"的情况. 有时候是因为其frame的size设置...

  • 增加点击区域

    项目中很多时候需要扩大点击(交互)区域,或子试图超出了父视图后,无法点击或交互等,我们可以通过响应者链-(UIVi...

  • 增加可点击区域

    UIView+hitTest.h #import @interfaceUIView (enlargeZone)-...

  • 扩大UIButton的点击区域

    创建一个UIButton的类别添加到项目中 在需要扩大按钮的点击范围处调用 注意:按钮扩大的范围必须在父视图内,超...

  • UIButton实现区域外点击

    UIButton实现区域外点击 今天项目开发中偶然需要这个功能,一个按钮区域大小外也要能够点击响应 因为项目是很早...

  • UIButton扩大默认点击区域

    在iOS开发中,经常遇到按钮的点击区域太小,无法达到一个良好的交互效果,通过如下方式扩大点击区域:

网友评论

      本文标题:增加UIButton 点击区域

      本文链接:https://www.haomeiwen.com/subject/wxeedftx.html