I'm currently trying to create a button that has the shape of a hexagon but the button still reacts to taps that are outside of its shape.
I successfully created the shape by overriding the func draw(_ rect: CGRect)
(see below) but unfortunately I can't figure out how to restrict the tappable area to the shape.
I also tried to set clipsToBound
to true
but that didn't help either.
override func draw(_ rect: CGRect) {
let radiusOuterCircle: CGFloat = self.frame.width / 2
let centerXY = self.frame.width / 2
let initialPoint = CGPoint(x: 0, y: centerXY)
let shapePath = UIBezierPath()
shapePath.move(to: initialPoint)
let secondPoint = CGPoint(x: radiusOuterCircle / 2, y: abs(tan(CGFloat.pi / 3) * (radiusOuterCircle / 2)) + centerXY)
let thirdPoint = CGPoint(x: radiusOuterCircle / 2 * 3, y: secondPoint.y)
let fourthPoint = CGPoint(x: radiusOuterCircle * 2, y: centerXY)
let fifthPoint = CGPoint(x: thirdPoint.x, y: centerXY - abs(tan(CGFloat.pi / 3) * (radiusOuterCircle / 2)))
let sixthPoint = CGPoint(x: centerXY / 2, y: fifthPoint.y)
shapePath.addLine(to: secondPoint)
shapePath.addLine(to: thirdPoint)
shapePath.addLine(to: fourthPoint)
shapePath.addLine(to: fifthPoint)
shapePath.addLine(to: sixthPoint)
shapePath.close()
UIColor(red: 247/255, green: 204/255, blue: 47/255, alpha: 1.0).set()
shapePath.fill()
}
Thank you very much :)