广告
返回顶部
首页 > 资讯 > 移动开发 >iOS UIButton扩大按钮响应区域的解决方法
  • 363
分享到

iOS UIButton扩大按钮响应区域的解决方法

iOSUIButton响应区域 2022-05-17 03:05:46 363人浏览 薄情痞子
摘要

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

开发中有时会遇见设计图里按钮设计的特别小,这时会用到手动扩大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"
 
@implementation BiggerClickAreaButton
 
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
 CGRect bounds = self.bounds;
 //若原热区小于44x44,则放大热区,否则保持原大小不变
 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

使用时直接继承创建即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: iOS UIButton扩大按钮响应区域的解决方法

本文链接: https://www.lsjlt.com/news/30163.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作