简体   繁体   English

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

[英]How to disable TouchUpInside Action outside the boundary of button

This is my Xib. 这是我的锡伯族。 I just dragged a UIButton and changed its background color. 我只是拖动了一个UIButton并更改了其背景颜色。

在此处输入图片说明

I created a semi circle layer using this code 我使用此代码创建了一个半圆形图层

    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

and this is my output. 这是我的输出。

在此处输入图片说明

Its perfect as I want it. 我想要的完美。 But the problem is that this button is clickable outside its rounded area(as per actuall shape of button). 但是问题在于此按钮在其圆形区域之外是可单击的(根据按钮的实际形状)。 I want it to be clickable only as rounded shape. 我希望它只能单击为圆形。

How can I achieve this ? 我该如何实现? Thanks. 谢谢。

Create action of signature 创建签名动作

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

for your button 为您的按钮

Find out touch location using this answer UIButton TouchUpInside Touch Location 使用此答案找出触摸位置UIButton TouchUpInside触摸位置

Check if that location is contained in your CGPath using 使用以下命令检查该位置是否包含在您的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
  }
} 

it returns a bool if it contains point. 如果包含点,则返回布尔值。

ie if it contains point you can proceed otherwise you can return. 即如果它包含点,则可以继续进行,否则可以返回。

Hope it helps :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM