ios - 将 UIButton 的触摸限制在指定区域

标签 ios objective-c swift uibutton swift2

我敢肯定这听起来像是一个菜鸟问题,但不知何故我卡住了,我在这里需要帮助。考虑我的 uibutton,它的背景图像有一个随意的可见边界,当点击时触摸按钮边界内,而不是专门在随意可见的边界,这完全有道理。但是我想将其触摸限制在可见边界而不是整个按钮。请在附件中找到解释我想要触摸的地方(绿色勾选)和不喜欢的地方(红叉)的图像。在此先感谢您。

enter image description here

最佳答案

继承 UIButton 并实现 pointInside:withEvent 方法。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/pointInside:withEvent :

当系统想知道为事件指定的点是否在您的视野内时,将调用此方法。如果是,则简单地返回 YES,如果不是,则返回 NO。

您所需要的只是一种确定该点是否在您的 UIButton 单击区域中的方法。当返回 YES 时。

像下面这样使用 CGRects 数组的东西:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {

      // Only pass through of the point is in a specific area
      BOOL ret=NO;

      for (NSValue *value in self.passThroughAreas){
          CGRect rect=value.CGRectValue;
          if (CGRectContainsPoint(rect, point)){
              // Its in one of our pass through areas. Let the events pass.
              ret=YES;
              break;
          }
      }

    return ret;
}

您使用的矩形越多,它们越小,您的区域就越有针对性。如果您最终要使用太多,也可以使用更高级的东西。

关于ios - 将 UIButton 的触摸限制在指定区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32436954/

相关文章:

android - 将 flex 移动项目转换为 flex web 项目

ios - 如何通过 iPhone 中的应用程序打开电子邮件附件的弹出窗口。

ios - Xcode - 链接器命令失败并显示退出代码

swift - 当枚举符合协议(protocol) CustomStringConvertible 时,是否可以从变量中获取枚举描述?

swift - 将 Swift 项目从 Linux 导入到 Mac

iOS Metal : Unable to create compute pipeline state with function

ios - React Native iOS - 未找到 GoogleToolboxforMac

ios - 如何转换 uinavigationviewcontroller 的第一个 View

iphone - 单击开关时检索行表 ID?

ios - CustomIOS7AlertView横向模式