Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active clickable area for UIButton with rounded corners?

So I have created a button with a border in my storyboard. enter image description here

and then I rounded its corners and added a border color:

button.layer.cornerRadius = button.bounds.size.width / 2
button.layer.borderColor = greenColor

So the runtime result looks like this: enter image description here

However the user can tap slightly outside the area of the button (where the corners used to be) and still call the button function. Is there a way to restrict the enabled area of the button to just be the circle?

like image 739
LuKenneth Avatar asked Jun 03 '16 15:06

LuKenneth


1 Answers

With other answers you block the touch, I needed it to fall through. And its even easier:

1) Setup your preferred path (for me circle)

private var touchPath: UIBezierPath {return UIBezierPath(ovalIn: self.bounds)}

2) Override point inside function

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    return touchPath.contains(point)
}

All inside you UIButton subclass.

like image 162
Vojta Rujbr Avatar answered Sep 24 '22 05:09

Vojta Rujbr