iOS UIButton擴(kuò)大按鈕響應(yīng)區(qū)域的解決方法
在開發(fā)中有時(shí)會(huì)遇見設(shè)計(jì)圖里按鈕設(shè)計(jì)的特別小,這時(shí)會(huì)用到手動(dòng)擴(kuò)大UIButton的響應(yīng)范圍,下面有兩個(gè)解決辦法:
第一種方法:創(chuàng)建一個(gè)類目: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)建一個(gè)UIButton類,然后復(fù)寫pointInside方法
使用時(shí)繼承于此類即可
.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
使用時(shí)直接繼承創(chuàng)建即可。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS 開發(fā)之自定義按鈕實(shí)現(xiàn)文字圖片位置隨意定制
這篇文章主要介紹了IOS 開發(fā)之自定義按鈕實(shí)現(xiàn)文字圖片位置隨意定制的相關(guān)資料,這里附有實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-12-12iOS 10撥打系統(tǒng)電話彈出框延遲出現(xiàn)問題的解決
iOS10的到來,帶來了條幅和鎖屏界面的重新設(shè)計(jì),美觀又好看,再加上抬腕喚醒功能,查看需要的信息確實(shí)更便捷了,還能快捷回復(fù)一些通知,十分輕松,但同樣有問題,下面這篇文章主要給大家介紹了關(guān)于iOS 10撥打系統(tǒng)電話彈出框延遲出現(xiàn)問題的解決方法,需要的朋友可以參考下。2017-10-10IOS Object-C 中Runtime詳解及實(shí)例代碼
這篇文章主要介紹了IOS Object-C 中Runtime詳解及實(shí)例代碼的相關(guān)資料,OC中的對(duì)象其實(shí)在Runtime中都會(huì)用結(jié)構(gòu)體來表示,這個(gè)結(jié)構(gòu)體中包含了類名、成員變量列表、方法列表、協(xié)議列表、緩存等,需要的朋友可以參考下2017-03-03關(guān)于iOS中的各種顏色設(shè)置總結(jié)大全(推薦)
這篇文章主要給大家介紹了關(guān)于iOS中顏色設(shè)置的相關(guān)資料,其中包括導(dǎo)航欄、狀態(tài)欄、Tabbar、Button、TextField、AttributedString和通用部分的顏色設(shè)置方法示例,對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起看看吧。2017-09-09iOS應(yīng)用開發(fā)中視圖控件UIWindow的基本使用教程
這篇文章主要介紹了iOS應(yīng)用開發(fā)中視圖控件UIWindow的基本使用教程,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-02-02