button擴大響應區

方法一:

#import

@interface UIButton (EnlargeTouchArea)

- (void)setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left;

@end

#import "UIButton+EnlargeTouchArea.h"

#import

@implementation UIButton (EnlargeTouchArea)

static char topNameKey;

static char rightNameKey;

static char bottomNameKey;

static char leftNameKey;

- (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;

}

方法二:

Apple的iOS人機交互設計指南中指出,按鈕點擊熱區應不小于44x44pt,否則這個按鈕就會讓用戶覺得“很難用”,因為明明點擊上去了,卻沒有任何響應。

但我們有時做自定義Button的時候,設計圖上的給出按鈕尺寸明顯要小于這個數。例如我之前做過的自定義Slider上的Thumb只有12x12pt,做出來后我發現自己根本點不到按鈕……

這個問題在WWDC 2012 Session 216視頻中提到了一種解決方式。它重寫了按鈕中的pointInside方法,使得按鈕熱區不夠44×44大小的先自動縮放到44×44,再判斷觸摸點是否在新的熱區內。

- (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);

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容