简体   繁体   中英

How to disable TouchUpInside Action outside the boundary of button

This is my Xib. I just dragged a UIButton and changed its background color.

在此处输入图片说明

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

Check if that location is contained in your CGPath using

- (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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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