UIButton:使命中区域大于默认命中区域 [英] UIButton: Making the hit area larger than the default hit area
问题描述
我有一个问题处理UIButton及其命中区。我在界面生成器中使用信息黑暗按钮,但我发现击中面积对于某些人的手指来说不够大。
以编程方式或在Interface Builder中增加按钮的命中区域而不改变InfoButton图形的大小?
p>因为我使用的是背景图片,所以这些解决方案都不适合我。这里是一个解决方案,做一些有趣的目标c魔法,并提供一个最小的代码解决方案。
首先,添加一个类别到 UIButton
@interface UIButton(扩展)
@property ,assign)UIEdgeInsets hitTestEdgeInsets;
@end
UIButton + Extensions.m
#importUIButton + 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)];
注意:请记住导入类别( #importUIButton + Extensions。 h
)。
I have a question dealing with UIButton and its hit area. I am using the Info Dark button in interface builder, but I am finding that the hit area is not large enough for some people's fingers.
Is there a way to increase the hit area of a button either programmatically or in Interface Builder without changing the size of the InfoButton graphic?
Since I am using a background image, none of these solutions worked well for me. Here is a solution that does some fun objective-c magic and offers a drop in solution with minimal code.
First, add a category to UIButton
that overrides the hit test and also adds a property for expanding the hit test frame.
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
Once this class is added, all you need to do is set the edge insets of your button. Note that I chose to add the insets so if you want to make the hit area larger, you must use negative numbers.
[button setHitTestEdgeInsets:UIEdgeInsetsMake(-10, -10, -10, -10)];
Note: Remember to import the category (#import "UIButton+Extensions.h"
) in your classes.
这篇关于UIButton:使命中区域大于默认命中区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!