[英]How can I add a mask to a button so that the hidden area is not tappable?
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.我通过覆盖
func draw(_ rect: CGRect)
成功创建了形状(见下文),但不幸的是我无法弄清楚如何将可点击区域限制为形状。
I also tried to set clipsToBound
to true
but that didn't help either.我也尝试将
clipsToBound
设置为true
,但这也无济于事。
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:)非常感谢:)
https://stackoverflow.com/a/36877464/7615046 https://stackoverflow.com/a/36877464/7615046
This actually is the answer to my problem.这实际上是我的问题的答案。
Below you can fing my updated code (Swift 5.1) as the other code is not up to date anymore:您可以在下面找到我更新的代码(Swift 5.1),因为其他代码不再是最新的:
override func awakeFromNib() {
addTarget(self, action: #selector(touchDown(button:event:)), for: .touchDown)
}
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)
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()
}
@objc func touchDown(button: HexagonButton, event: UIEvent) {
if let touch = event.touches(for: button)?.first {
let location = touch.location(in: button)
if shapePath.contains(location) == false {
button.cancelTracking(with: nil)
}
}
}
All of this code belongs into the custom Button class.所有这些代码都属于自定义按钮 class。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.