The UIButton of iOS expands the response area of the button

Keywords: iOS less

In the development process, we sometimes encounter that the buttons in the design diagram are designed to be very small. In this case, we will manually expand the response range of UIButton. Here are two solutions:

 

The first method: create a category: UIButton+EnlargeTouchArea

.h file

#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 file

 

//
//  UIButton+EnlargeTouchArea.m
//  HeBeiFM
//
//  Created by Apple on 16/4/27.
//  Copyright © 2016 year 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

usage method:

    UIButton *button = [UIButton new];
    [button setEnlargeEdge:20];
    //perhaps
    [button setEnlargeEdgeWithTop:20 right:20 bottom:20 left:20];

 

 

Second, create a UIButton class directly, and then copy the pointInside method

Inherit from this class when using

.h file

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

.m file

#import "BiggerClickAreaButton.h"
 
@implementation BiggerClickAreaButton
 
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
    CGRect bounds = self.bounds;
    //If the primary heat area is less than 44 x44,Enlarge the hot area, otherwise keep the original size unchanged
    CGFloat widthDelta = MAX(44.0 - bounds.size.width, 0);
    CGFloat heightDelta = MAX(44.0 - bounds.size.height, 0);
    bounds = CGRectInset(bounds, -0.5 * widthDelta, -0.5 * heightDelta);
    return CGRectContainsPoint(bounds, point);
}
 
 
@end

Directly inherit and create when using

Posted by RMcLeod on Tue, 31 Dec 2019 05:27:56 -0800