如何在按钮边界外禁用TouchUpInside Action

Uma*_*zal 1 cashapelayer ios uibezierpath swift

这是我的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
Run Code Online (Sandbox Code Playgroud)

这是我的输出.

在此输入图像描述

它完美,因为我想要它.但问题是这个按钮可以在其圆形区域外点击(根据按钮的实际形状).我希望它只能作为圆形可点击.

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

San*_*man 6

创建签名操作

- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event{
//your code
} 
Run Code Online (Sandbox Code Playgroud)

为你的按钮

使用此答案 找出触摸位置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
  }
} 
Run Code Online (Sandbox Code Playgroud)

如果它包含点,它返回一个bool.

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

希望能帮助到你 :)