设为首页 加入收藏

TOP

iOS之UIButton扩大按钮的响应区域(一)
2019-08-26 06:56:33 】 浏览:48
Tags:iOS UIButton 扩大 按钮 响应 区域

在开发中有时会遇见设计图里按钮设计的特别小,这时会用到手动扩大UIButton的响应范围,下面有两个解决办法:

 

第一种方法:创建一个类目:UIButton+EnlargeTouchArea 

.h文件

#import <UIKit/UIKit.h>
 
@interface UIButton (EnlargeTouchArea)
 
- (void)setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left;
 
- (void)setEnlargeEdge:(CGFloat) size;
 
@end

.m文件

 

//
//  UIButton+EnlargeTouchArea.m
//  HeBeiFM
//
//  Created by Apple on 16/4/27.
//  Copyright © 2016年 Apple. 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;
 
- (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 *button = [UIButton new];
    [button setEnlargeEdge:20];
    //或者
    [button setEnlargeEdgeWithTop:20 right:20 bottom:20 left:20];

 

 

第二种:直接创建一个UIButton类,然后复写pointInside方法

使用时继承于此类即可

.h文件

#import <UIKit/UIKit.h>
 
@interface BiggerClickAreaButton : UIButton
 
@end

.m文件

#import "BiggerClickAreaButton.h&q
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS-iOS9系统SEGV_ACCERR问题处理.. 下一篇OC数组的简单使用、NSArray

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目