iOS UIButton擴(kuò)大按鈕響應(yīng)區(qū)域的解決方法
在開發(fā)中有時會遇見設(shè)計圖里按鈕設(shè)計的特別小,這時會用到手動擴(kuò)大UIButton的響應(yīng)范圍,下面有兩個解決辦法:
第一種方法:創(chuàng)建一個類目: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];
第二種:直接創(chuàng)建一個UIButton類,然后復(fù)寫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;
//若原熱區(qū)小于44x44,則放大熱區(qū),否則保持原大小不變
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
使用時直接繼承創(chuàng)建即可。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS開發(fā)中對文件目錄的訪問及管理的基本方法小結(jié)
這篇文章主要介紹了iOS開發(fā)中對文件目錄的訪問及管理的基本方法小結(jié),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10
詳解ios中自定義cell,自定義UITableViewCell
本篇文章主要介紹了ios中自定義cell,自定義UITableViewCell,非常具有實(shí)用價值,需要的朋友可以參考下。2016-12-12
iOS AVCaptureSession實(shí)現(xiàn)視頻錄制功能
這篇文章主要為大家詳細(xì)介紹了iOS AVCaptureSession實(shí)現(xiàn)視頻錄制功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
解決iOS調(diào)起微信支付顯示系統(tǒng)繁忙問題
這篇文章主要介紹了解決iOS調(diào)起微信支付顯示系統(tǒng)繁忙問題,需要的朋友可以參考下2016-12-12

