国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS UIButton擴大按鈕響應區域的解決方法

2020-07-26 02:24:15
字體:
來源:轉載
供稿:網友

在開發中有時會遇見設計圖里按鈕設計的特別小,這時會用到手動擴大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

使用時直接繼承創建即可。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 溧水县| 萨嘎县| 民丰县| 饶阳县| 涞源县| 松桃| 潞西市| 漯河市| 那曲县| 成安县| 广平县| 曲麻莱县| 融水| 米林县| 兴山县| 抚松县| 贵州省| 大同县| 中方县| 禄劝| 崇左市| 东兴市| 内丘县| 江阴市| 乌拉特中旗| 衡东县| 仪陇县| 电白县| 遂宁市| 枝江市| 永康市| 交口县| 广安市| 宣武区| 瑞金市| 孝义市| 灯塔市| 汤原县| 庄河市| 华亭县| 梁平县|