繁体   English   中英

如何为按钮添加遮罩以使隐藏区域不可点击?

[英]How can I add a mask to a button so that the hidden area is not tappable?

我目前正在尝试创建一个具有六边形形状的按钮,但该按钮仍会对其形状之外的点击做出反应。
我通过覆盖func draw(_ rect: CGRect)成功创建了形状(见下文),但不幸的是我无法弄清楚如何将可点击区域限制为形状。
我也尝试将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()
    }

非常感谢:)

https://stackoverflow.com/a/36877464/7615046
这实际上是我的问题的答案。

您可以在下面找到我更新的代码(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)
            }
        }
    }

所有这些代码都属于自定义按钮 class。

暂无
暂无

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

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