• <legend id='YKLg6'><style id='YKLg6'><dir id='YKLg6'><q id='YKLg6'></q></dir></style></legend>

        <i id='YKLg6'><tr id='YKLg6'><dt id='YKLg6'><q id='YKLg6'><span id='YKLg6'><b id='YKLg6'><form id='YKLg6'><ins id='YKLg6'></ins><ul id='YKLg6'></ul><sub id='YKLg6'></sub></form><legend id='YKLg6'></legend><bdo id='YKLg6'><pre id='YKLg6'><center id='YKLg6'></center></pre></bdo></b><th id='YKLg6'></th></span></q></dt></tr></i><div id='YKLg6'><tfoot id='YKLg6'></tfoot><dl id='YKLg6'><fieldset id='YKLg6'></fieldset></dl></div>
        <tfoot id='YKLg6'></tfoot>
          <bdo id='YKLg6'></bdo><ul id='YKLg6'></ul>

      1. <small id='YKLg6'></small><noframes id='YKLg6'>

        UIButton:使点击区域大于默认点击区域

        时间:2023-07-09

            <small id='MsmXc'></small><noframes id='MsmXc'>

            <i id='MsmXc'><tr id='MsmXc'><dt id='MsmXc'><q id='MsmXc'><span id='MsmXc'><b id='MsmXc'><form id='MsmXc'><ins id='MsmXc'></ins><ul id='MsmXc'></ul><sub id='MsmXc'></sub></form><legend id='MsmXc'></legend><bdo id='MsmXc'><pre id='MsmXc'><center id='MsmXc'></center></pre></bdo></b><th id='MsmXc'></th></span></q></dt></tr></i><div id='MsmXc'><tfoot id='MsmXc'></tfoot><dl id='MsmXc'><fieldset id='MsmXc'></fieldset></dl></div>
              <tbody id='MsmXc'></tbody>
              <bdo id='MsmXc'></bdo><ul id='MsmXc'></ul>

              • <tfoot id='MsmXc'></tfoot>

                1. <legend id='MsmXc'><style id='MsmXc'><dir id='MsmXc'><q id='MsmXc'></q></dir></style></legend>
                  本文介绍了UIButton:使点击区域大于默认点击区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个关于 UIButton 及其点击区域的问题.我正在使用界面生成器中的信息暗按钮,但我发现点击区域对于某些人的手指来说不够大.

                  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.

                  有没有办法在不改变 InfoButton 图形大小的情况下以编程方式或在 Interface Builder 中增加按钮的点击区域?

                  推荐答案

                  因为我使用的是背景图片,所以这些解决方案都不适合我.这是一个解决方案,它可以实现一些有趣的 Objective-c 魔法,并提供了一个使用最少代码的解决方案.

                  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.

                  首先,在 UIButton 中添加一个覆盖命中测试的类别,并添加一个用于扩展命中测试帧的属性.

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

                  注意:记得在你的类中导入类别(#import "UIButton+Extensions.h").

                  Note: Remember to import the category (#import "UIButton+Extensions.h") in your classes.

                  这篇关于UIButton:使点击区域大于默认点击区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:子类 UIButton 添加属性 下一篇:如何向 UIButton 添加多行文本?

                  相关文章

                  1. <small id='wGED9'></small><noframes id='wGED9'>

                      <bdo id='wGED9'></bdo><ul id='wGED9'></ul>
                  2. <legend id='wGED9'><style id='wGED9'><dir id='wGED9'><q id='wGED9'></q></dir></style></legend>
                  3. <i id='wGED9'><tr id='wGED9'><dt id='wGED9'><q id='wGED9'><span id='wGED9'><b id='wGED9'><form id='wGED9'><ins id='wGED9'></ins><ul id='wGED9'></ul><sub id='wGED9'></sub></form><legend id='wGED9'></legend><bdo id='wGED9'><pre id='wGED9'><center id='wGED9'></center></pre></bdo></b><th id='wGED9'></th></span></q></dt></tr></i><div id='wGED9'><tfoot id='wGED9'></tfoot><dl id='wGED9'><fieldset id='wGED9'></fieldset></dl></div>

                      <tfoot id='wGED9'></tfoot>