【问题标题】:How to disable TouchUpInside Action outside the boundary of button如何在按钮边界外禁用 TouchUpInside Action
【发布时间】:2019-01-17 19:48:33
【问题描述】:

这是我的 Xib。我只是拖动了一个 UIButton 并更改了它的背景颜色。

我使用此代码创建了一个半圆形图层

    let circlePath = UIBezierPath.init(arcCenter: CGPointMake(mybutton.bounds.size.width / 4, 0), radius: mybutton.bounds.size.height, startAngle: 0.0, endAngle: CGFloat(M_PI), clockwise: true)
    let circleShape = CAShapeLayer()
    circleShape.path = circlePath.CGPath
    mybutton.layer.mask = circleShape

这是我的输出。

如我所愿,完美无缺。但问题是这个按钮在其圆形区域之外是可点击的(根据按钮的实际形状)。我希望它只能作为圆形可点击。

我怎样才能做到这一点?谢谢。

【问题讨论】:

  • sanman 的方法会奏效。你也可以尝试使用hitTest:

标签: ios swift uibezierpath cashapelayer


【解决方案1】:

创建签名动作

- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event{
//your code
} 

为您的按钮

使用此答案找出触摸位置 UIButton TouchUpInside Touch Location

使用检查该位置是否包含在您的 CGPath 中

- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event{
  //find location point using above answer link
  if([button.layer.mask containsPoint: location])
  {
   //proceed with the code
  }
} 

如果它包含点,则返回一个布尔值。

即如果它包含点,您可以继续,否则您可以返回。

希望对你有帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 2020-03-31
    相关资源
    最近更新 更多