我有一个关于UIButton及其命中区域的问题。我正在使用界面构建器中的“信息暗”按钮,但我发现命中区域对于某些人的手指来说不够大。
有没有办法以编程方式或在Interface Builder中增加按钮的命中区域而不改变InfoButton图形的大小?
由于我使用的是背景图像,因此这些解决方案都不适合我。这是一个解决方案,可以实现一些有趣的Objective-c魔术,并提供最少代码的解决方案。
首先,向UIButton
添加一个覆盖命中测试的类别,并添加一个用于扩展命中测试帧的属性。
的UIButton + Extensions.h
@interface UIButton (Extensions)
@property(nonatomic, assign) UIEdgeInsets hitTestEdgeInsets;
@end
的UIButton + Extensions.m
#import "UIButton+Extensions.h"
#import <objc/runtime.h>
@implementation UIButton (Extensions)
@dynamic hitTestEdgeInsets;
static const NSString *KEY_HIT_TEST_EDGE_INSETS = @"HitTestEdgeInsets";
-(void)setHitTestEdgeInsets:(UIEdgeInsets)hitTestEdgeInsets {
NSValue *value = [NSValue value:&hitTestEdgeInsets withObjCType:@encode(UIEdgeInsets)];
objc_setAssociatedObject(self, &KEY_HIT_TEST_EDGE_INSETS, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UIEdgeInsets)hitTestEdgeInsets {
NSValue *value = objc_getAssociatedObject(self, &KEY_HIT_TEST_EDGE_INSETS);
if(value) {
UIEdgeInsets edgeInsets; [value getValue:&edgeInsets]; return edgeInsets;
}else {
return UIEdgeInsetsZero;
}
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
if(UIEdgeInsetsEqualToEdgeInsets(self.hitTestEdgeInsets, UIEdgeInsetsZero) || !self.enabled || self.hidden) {
return [super pointInside:point withEvent:event];
}
CGRect relativeFrame = self.bounds;
CGRect hitFrame = UIEdgeInsetsInsetRect(relativeFrame, self.hitTestEdgeInsets);
return CGRectContainsPoint(hitFrame, point);
}
@end
添加此类后,您需要做的就是设置按钮的边缘插入。请注意,我选择添加插图,因此如果要使命中区域更大,则必须使用负数。
[button setHitTestEdgeInsets:UIEdgeInsetsMake(-10, -10, -10, -10)];
注意:请记住在类中导入类别(#import "UIButton+Extensions.h"
)。
这对我有用:
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
// set the image (here with a size of 32 x 32)
[button setImage: [UIImage imageNamed: @"myimage.png"] forState: UIControlStateNormal];
// just set the frame of the button (here 64 x 64)
[button setFrame: CGRectMake(xPositionOfMyButton, yPositionOfMyButton, 64, 64)];
不要改变UIButton的行为。
@interface ExtendedHitButton: UIButton
+ (instancetype) extendedHitButton;
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
@end
@implementation ExtendedHitButton
+ (instancetype) extendedHitButton {
return (ExtendedHitButton *) [ExtendedHitButton buttonWithType:UIButtonTypeCustom];
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
CGRect relativeFrame = self.bounds;
UIEdgeInsets hitTestEdgeInsets = UIEdgeInsetsMake(-44, -44, -44, -44);
CGRect hitFrame = UIEdgeInsetsInsetRect(relativeFrame, hitTestEdgeInsets);
return CGRectContainsPoint(hitFrame, point);
}
@end
我通过调整-[UIView pointInside:withEvent:]
使用更通用的方法。这允许我修改任何UIView
上的命中测试行为,而不仅仅是UIButton
。
通常,按钮放置在容器视图内,这也限制了命中测试。例如,当按钮位于容器视图的顶部并且您想要向上扩展触摸目标时,您还必须扩展容器视图的触摸目标。
@interface UIView(Additions)
@property(nonatomic) UIEdgeInsets hitTestEdgeInsets;
@end
@implementation UIView(Additions)
+ (void)load {
Swizzle(self, @selector(pointInside:withEvent:), @selector(myPointInside:withEvent:));
}
- (BOOL)myPointInside:(CGPoint)point withEvent:(UIEvent *)event {
if(UIEdgeInsetsEqualToEdgeInsets(self.hitTestEdgeInsets, UIEdgeInsetsZero) || self.hidden ||
([self isKindOfClass:UIControl.class] && !((UIControl*)self).enabled))
{
return [self myPointInside:point withEvent:event]; // original implementation
}
CGRect hitFrame = UIEdgeInsetsInsetRect(self.bounds, self.hitTestEdgeInsets);
hitFrame.size.width = MAX(hitFrame.size.width, 0); // don't allow negative sizes
hitFrame.size.height = MAX(hitFrame.size.height, 0);
return CGRectContainsPoint(hitFrame, point);
}
static char hitTestEdgeInsetsKey;
- (void)setHitTestEdgeInsets:(UIEdgeInsets)hitTestEdgeInsets {
objc_setAssociatedObject(self, &hitTestEdgeInsetsKey, [NSValue valueWithUIEdgeInsets:hitTestEdgeInsets], OBJC_ASSOCIATION_RETAIN);
}
- (UIEdgeInsets)hitTestEdgeInsets {
return [objc_getAssociatedObject(self, &hitTestEdgeInsetsKey) UIEdgeInsetsValue];
}
void Swizzle(Class c, SEL orig, SEL new) {
Method origMethod = class_getInstanceMethod(c, orig);
Method newMethod = class_getInstanceMethod(c, new);
if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
else
method_exchangeImplementations(origMethod, newMethod);
}
@end
这种方法的好处是你可以通过添加用户定义的运行时属性在故事板中使用它。遗憾的是,UIEdgeInsets
不是直接可用作为那里的类型,但由于CGRect
也包含一个带有四个CGFloat
的结构,它通过选择“Rect”并填写如下值来完美地工作:{{top, left}, {bottom, right}}
。
好吧,你可以将你的UIButton放在一个透明且略大的UIView中,然后像在UIButton中那样捕获UIView实例上的触摸事件。这样,您仍然可以使用按钮,但触摸区域更大。如果用户触摸视图而不是按钮,您将手动处理选定和突出显示的状态。
其他可能性涉及使用UIImage而不是UIButton。
我已经能够以编程方式增加信息按钮的命中区域。 “i”图形不会改变比例并保持在新按钮框架的中心。
在Interface Builder中,信息按钮的大小似乎固定为18x19 [*]。通过将其连接到IBOutlet,我能够在代码中更改其帧大小而没有任何问题。
static void _resizeButton( UIButton *button )
{
const CGRect oldFrame = infoButton.frame;
const CGFloat desiredWidth = 44.f;
const CGFloat margin =
( desiredWidth - CGRectGetWidth( oldFrame ) ) / 2.f;
infoButton.frame = CGRectInset( oldFrame, -margin, -margin );
}
[*]:iOS的更高版本似乎增加了信息按钮的命中区域。
我在Swift中使用以下类,也启用Interface Builder属性来调整边距:
@IBDesignable
class ALExtendedButton: UIButton {
@IBInspectable var touchMargin:CGFloat = 20.0
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
var extendedArea = CGRectInset(self.bounds, -touchMargin, -touchMargin)
return CGRectContainsPoint(extendedArea, point)
}
}
这是我的Swift 3解决方案(基于这篇博文:qazxsw poi)
http://bdunagan.com/2010/03/01/iphone-tip-larger-hit-area-for-uibutton/
Chase的自定义命中测试是作为UIButton的子类实现的。用Objective-C编写。
它似乎适用于class ExtendedHitAreaButton: UIButton {
@IBInspectable var hitAreaExtensionSize: CGSize = CGSize(width: -10, height: -10)
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let extendedFrame: CGRect = bounds.insetBy(dx: hitAreaExtensionSize.width, dy: hitAreaExtensionSize.height)
return extendedFrame.contains(point) ? self : nil
}
}
和init
构造函数。对于我的需求它是完美的,但由于buttonWithType:
的子类化可能很毛茸茸,我有兴趣知道是否有人有它的错。
UIButton
#import <UIKit/UIKit.h>
@interface CustomHitAreaButton : UIButton
- (void)setHitTestEdgeInsets:(UIEdgeInsets)hitTestEdgeInsets;
@end
通过覆盖继承UIButton实现。
Swift 2.2:
#import "CustomHitAreaButton.h"
@interface CustomHitAreaButton()
@property (nonatomic, assign) UIEdgeInsets hitTestEdgeInsets;
@end
@implementation CustomHitAreaButton
- (instancetype)initWithFrame:(CGRect)frame {
if(self = [super initWithFrame:frame]) {
self.hitTestEdgeInsets = UIEdgeInsetsZero;
}
return self;
}
-(void)setHitTestEdgeInsets:(UIEdgeInsets)hitTestEdgeInsets {
self->_hitTestEdgeInsets = hitTestEdgeInsets;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
if(UIEdgeInsetsEqualToEdgeInsets(self.hitTestEdgeInsets, UIEdgeInsetsZero) || !self.enabled || self.hidden) {
return [super pointInside:point withEvent:event];
}
CGRect relativeFrame = self.bounds;
CGRect hitFrame = UIEdgeInsetsInsetRect(relativeFrame, self.hitTestEdgeInsets);
return CGRectContainsPoint(hitFrame, point);
}
@end
WJBackgroundInsetButton.h
// don't forget that negative values are for outset
_button.hitOffset = UIEdgeInsets(top: -10, left: -10, bottom: -10, right: -10)
...
class UICustomButton: UIButton {
var hitOffset = UIEdgeInsets()
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
guard hitOffset != UIEdgeInsetsZero && enabled && !hidden else {
return super.pointInside(point, withEvent: event)
}
return UIEdgeInsetsInsetRect(bounds, hitOffset).contains(point)
}
}
WJBackgroundInsetButton.m
#import <UIKit/UIKit.h>
@interface WJBackgroundInsetButton : UIButton {
UIEdgeInsets backgroundEdgeInsets_;
}
@property (nonatomic) UIEdgeInsets backgroundEdgeInsets;
@end
只需在界面构建器中设置图像边缘插入值即可。
切勿覆盖类别中的方法。子类按钮和覆盖#import "WJBackgroundInsetButton.h"
@implementation WJBackgroundInsetButton
@synthesize backgroundEdgeInsets = backgroundEdgeInsets_;
-(CGRect) backgroundRectForBounds:(CGRect)bounds {
CGRect sup = [super backgroundRectForBounds:bounds];
UIEdgeInsets insets = self.backgroundEdgeInsets;
CGRect r = UIEdgeInsetsInsetRect(sup, insets);
return r;
}
@end
。例如,如果您的按钮侧小于44像素(建议作为最小可点击区域),请使用以下命令:
- pointInside:withEvent:
我为此目的做了一个- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
return (ABS(point.x - CGRectGetMidX(self.bounds)) <= MAX(CGRectGetMidX(self.bounds), 22)) && (ABS(point.y - CGRectGetMidY(self.bounds)) <= MAX(CGRectGetMidY(self.bounds), 22));
}
。
您可以选择使用library类别,不需要子类化:
UIView
或者你可以继承你的UIView或UIButton并设置@interface UIView (KGHitTesting)
- (void)setMinimumHitTestWidth:(CGFloat)width height:(CGFloat)height;
@end
和/或minimumHitTestWidth
。然后,您的按钮命中测试区域将由这两个值表示。
就像其他解决方案一样,它使用minimumHitTestHeight
方法。当iOS执行命中测试时调用该方法。 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
博客文章对iOS命中测试的工作方式有很好的描述。
https://github.com/kgaidis/KGHitTestingViews
您也可以只创建子类并使用Interface Builder而无需编写任何代码:
基于giaset上面的答案(我找到了最优雅的解决方案),这里是swift 3版本:
@interface KGHitTestingButton : UIButton <KGHitTesting>
@property (nonatomic) CGFloat minimumHitTestHeight;
@property (nonatomic) CGFloat minimumHitTestWidth;
@end
我在tableviewcell.accessoryView中使用这个技巧来扩大其触摸区域
import UIKit
fileprivate let minimumHitArea = CGSize(width: 44, height: 44)
extension UIButton {
open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
// if the button is hidden/disabled/transparent it can't be hit
if isHidden || !isUserInteractionEnabled || alpha < 0.01 { return nil }
// increase the hit frame to be at least as big as `minimumHitArea`
let buttonSize = bounds.size
let widthToAdd = max(minimumHitArea.width - buttonSize.width, 0)
let heightToAdd = max(minimumHitArea.height - buttonSize.height, 0)
let largerFrame = bounds.insetBy(dx: -widthToAdd / 2, dy: -heightToAdd / 2)
// perform hit test on larger frame
return (largerFrame.contains(point)) ? self : nil
}
}
我刚刚在swift 2.2中完成了@Chase #pragma mark - Touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
CGRect accessoryViewTouchRect = CGRectInset(self.accessoryView.frame, -15, -15);
if(!CGRectContainsPoint(accessoryViewTouchRect, location))
[super touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
CGRect accessoryViewTouchRect = CGRectInset(self.accessoryView.frame, -15, -15);
if(CGRectContainsPoint(accessoryViewTouchRect, location) && [self.accessoryView isKindOfClass:[UIButton class]])
{
[(UIButton *)self.accessoryView sendActionsForControlEvents:UIControlEventTouchUpInside];
}
else
[super touchesEnded:touches withEvent:event];
}
的端口
solution
你可以像这样用
import Foundation
import ObjectiveC
private var hitTestEdgeInsetsKey: UIEdgeInsets = UIEdgeInsetsZero
extension UIButton {
var hitTestEdgeInsets:UIEdgeInsets {
get {
let inset = objc_getAssociatedObject(self, &hitTestEdgeInsetsKey) as? NSValue ?? NSValue(UIEdgeInsets: UIEdgeInsetsZero)
return inset.UIEdgeInsetsValue()
}
set {
let inset = NSValue(UIEdgeInsets: newValue)
objc_setAssociatedObject(self, &hitTestEdgeInsetsKey, inset, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
public override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
guard !UIEdgeInsetsEqualToEdgeInsets(hitTestEdgeInsets, UIEdgeInsetsZero) && self.enabled == true && self.hidden == false else {
return super.pointInside(point, withEvent: event)
}
let relativeFrame = self.bounds
let hitFrame = UIEdgeInsetsInsetRect(relativeFrame, hitTestEdgeInsets)
return CGRectContainsPoint(hitFrame, point)
}
}
有关其他参考,请参阅button.hitTestEdgeInsets = UIEdgeInsetsMake(-10, -10, -10, -10)
@ antoine的答案格式为Swift 4
https://stackoverflow.com/a/13067285/1728552
我已经跟踪了Chase的响应并且它工作得很好,当你创建的arrea太大,比取消选择按钮的区域大时(如果区域不大)它不会调用UIControlEventTouchUpInside事件的选择器。
我认为任何方向或类似的东西都超过200。
我对这个游戏来说太迟了,但是想要权衡一个可以解决你的问题的简单技术。这是我的典型程序化UIButton片段:
class ExtendedHitButton: UIButton
{
override func point( inside point: CGPoint, with event: UIEvent? ) -> Bool
{
let relativeFrame = self.bounds
let hitTestEdgeInsets = UIEdgeInsetsMake( -44, -44, -44, -44 ) // Apple recommended hit target
let hitFrame = UIEdgeInsetsInsetRect( relativeFrame, hitTestEdgeInsets )
return hitFrame.contains( point );
}
}
我正在为我的按钮加载一个透明的png图像并设置背景图像。我正在根据UIImage设置框架,并为视网膜缩放50%。好吧,也许您同意上述与否,但是如果你想使命中区域变得更大并且让自己头疼:
我做了什么,在photoshop中打开图像,只需将画布大小增加到120%并保存。实际上,你只是通过透明像素使图像更大。
只是一种方法。
@jlajlar上面的回答似乎很好而且直截了当但与Xamarin.iOS不匹配,所以我把它转换成了Xamarin。如果您在Xamarin iOS上寻找解决方案,那么它就是:
UIImage *arrowImage = [UIImage imageNamed:@"leftarrow"];
arrowButton = [[UIButton alloc] initWithFrame:CGRectMake(15.0, self.frame.size.height-35.0, arrowImage.size.width/2, arrowImage.size.height/2)];
[arrowButton setBackgroundImage:arrowImage forState:UIControlStateNormal];
[arrowButton addTarget:self action:@selector(onTouchUp:) forControlEvents:UIControlEventTouchUpOutside];
[arrowButton addTarget:self action:@selector(onTouchDown:) forControlEvents:UIControlEventTouchDown];
[arrowButton addTarget:self action:@selector(onTap:) forControlEvents:UIControlEventTouchUpInside];
[arrowButton addTarget:self action:@selector(onTouchUp:) forControlEvents:UIControlEventTouchDragExit];
[arrowButton setUserInteractionEnabled:TRUE];
[arrowButton setAdjustsImageWhenHighlighted:NO];
[arrowButton setTag:1];
[self addSubview:arrowButton];
您可以将此方法添加到覆盖UIView或UIImageView的类中。这很好用:)
这些答案都不适合我,因为我在该按钮上使用背景图像和标题。此外,随着屏幕尺寸的变化,按钮将调整大小。
相反,我通过使png透明区域变大来扩大抽头区域。
这是使用Swift中的Extensions的优雅解决方案。根据Apple的人机界面指南(https://developer.apple.com/ios/human-interface-guidelines/visual-design/layout/),它为所有UIButton提供了至少44x44点的命中区域
斯威夫特2:
private let minimumHitArea = CGSizeMake(44, 44)
extension UIButton {
public override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
// if the button is hidden/disabled/transparent it can't be hit
if self.hidden || !self.userInteractionEnabled || self.alpha < 0.01 { return nil }
// increase the hit frame to be at least as big as `minimumHitArea`
let buttonSize = self.bounds.size
let widthToAdd = max(minimumHitArea.width - buttonSize.width, 0)
let heightToAdd = max(minimumHitArea.height - buttonSize.height, 0)
let largerFrame = CGRectInset(self.bounds, -widthToAdd / 2, -heightToAdd / 2)
// perform hit test on larger frame
return (CGRectContainsPoint(largerFrame, point)) ? self : nil
}
}
斯威夫特3:
fileprivate let minimumHitArea = CGSize(width: 100, height: 100)
extension UIButton {
open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
// if the button is hidden/disabled/transparent it can't be hit
if self.isHidden || !self.isUserInteractionEnabled || self.alpha < 0.01 { return nil }
// increase the hit frame to be at least as big as `minimumHitArea`
let buttonSize = self.bounds.size
let widthToAdd = max(minimumHitArea.width - buttonSize.width, 0)
let heightToAdd = max(minimumHitArea.height - buttonSize.height, 0)
let largerFrame = self.bounds.insetBy(dx: -widthToAdd / 2, dy: -heightToAdd / 2)
// perform hit test on larger frame
return (largerFrame.contains(point)) ? self : nil
}
}
此Swift版本允许您为所有UIButton定义最小命中大小。至关重要的是,当UIButton被隐藏时,它也会处理这种情况,很多人都会忽略这一点。
public override bool PointInside (CoreGraphics.CGPoint point, UIEvent uievent)
{
var margin = -10f;
var area = this.Bounds;
var expandedArea = area.Inset(margin, margin);
return expandedArea.Contains(point);
}
你也可以继承UIButton
或自定义UIView
并覆盖point(inside:with:)
,例如:
斯威夫特3
override func point(inside point: CGPoint, with _: UIEvent?) -> Bool {
let margin: CGFloat = 5
let area = self.bounds.insetBy(dx: -margin, dy: -margin)
return area.contains(point)
}
Objective-C的
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
CGFloat margin = 5.0;
CGRect area = CGRectInset(self.bounds, -margin, -margin);
return CGRectContainsPoint(area, point);
}
这是Chase在Swift 3.0中的UIButton + Extensions。
import UIKit
private var pTouchAreaEdgeInsets: UIEdgeInsets = .zero
extension UIButton {
var touchAreaEdgeInsets: UIEdgeInsets {
get {
if let value = objc_getAssociatedObject(self, &pTouchAreaEdgeInsets) as? NSValue {
var edgeInsets: UIEdgeInsets = .zero
value.getValue(&edgeInsets)
return edgeInsets
}
else {
return .zero
}
}
set(newValue) {
var newValueCopy = newValue
let objCType = NSValue(uiEdgeInsets: .zero).objCType
let value = NSValue(&newValueCopy, withObjCType: objCType)
objc_setAssociatedObject(self, &pTouchAreaEdgeInsets, value, .OBJC_ASSOCIATION_RETAIN)
}
}
open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
if UIEdgeInsetsEqualToEdgeInsets(self.touchAreaEdgeInsets, .zero) || !self.isEnabled || self.isHidden {
return super.point(inside: point, with: event)
}
let relativeFrame = self.bounds
let hitFrame = UIEdgeInsetsInsetRect(relativeFrame, self.touchAreaEdgeInsets)
return hitFrame.contains(point)
}
}
要使用它,您可以:
button.touchAreaEdgeInsets = UIEdgeInsets(top: -10, left: -10, bottom: -10, right: -10)
不要在图像中设置backgroundImage
属性,设置imageView
属性。此外,请确保您在imageView.contentMode
设置了UIViewContentModeCenter
。
我建议在信息按钮上放置一个自定义类型的UIButton。将自定义按钮的大小调整为您希望命中区域的大小。从那里你有两个选择:
我在Swift 3上的解决方案:
class MyButton: UIButton {
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
let relativeFrame = self.bounds
let hitTestEdgeInsets = UIEdgeInsetsMake(-25, -25, -25, -25)
let hitFrame = UIEdgeInsetsInsetRect(relativeFrame, hitTestEdgeInsets)
return hitFrame.contains(point)
}
}
提出的答案没有错;但是我想扩展jlarjlar's answer,因为它具有惊人的潜力,可以为其他控件(例如SearchBar)增加相同问题的价值。这是因为因为pointInside附加到UIView,所以可以对任何控件进行子类化以改善触摸区域。这个答案还展示了如何实现完整解决方案的完整示例。
为您的按钮(或任何控件)创建一个新的子类
#import <UIKit/UIKit.h>
@interface MNGButton : UIButton
@end
接下来重写子类实现中的pointInside方法
@implementation MNGButton
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
//increase touch area for control in all directions by 20
CGFloat margin = 20.0;
CGRect area = CGRectInset(self.bounds, -margin, -margin);
return CGRectContainsPoint(area, point);
}
@end
在storyboard / xib文件中,选择有问题的控件并打开标识检查器,然后键入自定义类的名称。
在包含按钮的场景的UIViewController类中,将按钮的类类型更改为子类的名称。
@property (weak, nonatomic) IBOutlet MNGButton *helpButton;
将您的storyboard / xib按钮链接到属性IBOutlet,您的触摸区域将扩展为适合子类中定义的区域。
除了用pointInside method和CGRectInset方法覆盖CGRectContainsPoint之外,还应花时间检查CGGeometry以扩展任何UIView子类的矩形触摸区域。您还可以在NSHipster上找到关于CGGeometry用例的一些很好的提示。
例如,可以使用上述方法使触摸区域不规则,或者仅选择使宽度触摸区域为水平触摸区域的两倍:
CGRect area = CGRectInset(self.bounds, -(2*margin), -margin);
注意:替换任何UI类控件应该在扩展不同控件(或任何UIView子类,如UIImageView等)的触摸区域时产生类似的结果。