如何使用UibezierPath绘制Uiview或Uibutton

发布于 2025-02-11 15:27:01 字数 1047 浏览 1 评论 0原文

我想绘制图像中显示的Uiview或Uibutton。

如何绘制Swift? 我在这方面做了很多r $ d,我得到的是uibezierpath。 因此,如何使用UibezierPath绘制。我无法准确地绘制这一点。 请帮助我像这样画画。

我想为此创建一个单独的类,以便可以继承任何地方。

先感谢您。

我尝试了这样的事情。

extension UIButton {
    func roundCorners(corners: UIRectCorner, radius: Int = 8) {
        let maskPath1 = UIBezierPath(roundedRect: bounds,
                                     byRoundingCorners: corners,
                                     cornerRadii: CGSize(width: radius, height: radius))
        
        let maskLayer1 = CAShapeLayer()
        maskLayer1.frame = bounds
        maskLayer1.path = maskPath1.cgPath
        layer.mask = maskLayer1
    }
}

结果是

但我想要一个锐利的切割。

I want to draw a UIView or a UIButton like this one which is show in the image.
enter image description here

How it is possible to draw in swift?
I have done a lot of R $ D on this what I get is UIBezierPath.
So, How to draw using UIBezierPath. I am unable to draw exactly this.
Please help me to draw like this.

I want create a separate class for this so that I can inherit any where.

Thank you in advance.

I have tried something like this.

extension UIButton {
    func roundCorners(corners: UIRectCorner, radius: Int = 8) {
        let maskPath1 = UIBezierPath(roundedRect: bounds,
                                     byRoundingCorners: corners,
                                     cornerRadii: CGSize(width: radius, height: radius))
        
        let maskLayer1 = CAShapeLayer()
        maskLayer1.frame = bounds
        maskLayer1.path = maskPath1.cgPath
        layer.mask = maskLayer1
    }
}

Result is

enter image description here

But I want a sharp cut.Like first image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个ˇ局外人 2025-02-18 15:27:02

要绘制该形状,您需要使用maskPath1.move(to:pt),然后使用一系列maskPath1.addline(TO:pt)

您需要六分:

”在此处输入图像描述”

因此您可以这样编码:

extension UIButton {
    func angledCorners() {
        let maskLayer1 = CAShapeLayer()
        maskLayer1.frame = bounds
        
        let pt1: CGPoint = CGPoint(x: bounds.minX, y: bounds.maxY)
        let pt2: CGPoint = CGPoint(x: bounds.minX, y: bounds.midY)
        let pt3: CGPoint = CGPoint(x: bounds.minX + bounds.midY, y: bounds.minY)
        let pt4: CGPoint = CGPoint(x: bounds.maxX, y: bounds.minY)
        let pt5: CGPoint = CGPoint(x: bounds.maxX, y: bounds.midY)
        let pt6: CGPoint = CGPoint(x: bounds.maxX - bounds.midY, y: bounds.maxY)

        let maskPath1: UIBezierPath = UIBezierPath()
        
        maskPath1.move(to: pt1)
        maskPath1.addLine(to: pt2)
        maskPath1.addLine(to: pt3)
        maskPath1.addLine(to: pt4)
        maskPath1.addLine(to: pt5)
        maskPath1.addLine(to: pt6)
        maskPath1.close()

        maskLayer1.path = maskPath1.cgPath
        layer.mask = maskLayer1
    }
}

尽管如此,该方法的几个问题...当按钮框架更改时,掩码不会“自动加强”。因此,您必须等待调用button.angledCorners(),直到自动layout完成设置按钮的框架为止,您必须在框架更改时再次调用它。

如果您的明确框架大小,那不是问题。

但是,如果您的按钮会根据其他视图更改,或者可能在旋转设备时更改尺寸,那是一个麻烦……并且您可能希望使其成为自定义子类的功能。

To draw that shape, you want to use maskPath1.move(to: pt) and then a series of maskPath1.addLine(to: pt).

You'll need six points:

enter image description here

so you could code it like this:

extension UIButton {
    func angledCorners() {
        let maskLayer1 = CAShapeLayer()
        maskLayer1.frame = bounds
        
        let pt1: CGPoint = CGPoint(x: bounds.minX, y: bounds.maxY)
        let pt2: CGPoint = CGPoint(x: bounds.minX, y: bounds.midY)
        let pt3: CGPoint = CGPoint(x: bounds.minX + bounds.midY, y: bounds.minY)
        let pt4: CGPoint = CGPoint(x: bounds.maxX, y: bounds.minY)
        let pt5: CGPoint = CGPoint(x: bounds.maxX, y: bounds.midY)
        let pt6: CGPoint = CGPoint(x: bounds.maxX - bounds.midY, y: bounds.maxY)

        let maskPath1: UIBezierPath = UIBezierPath()
        
        maskPath1.move(to: pt1)
        maskPath1.addLine(to: pt2)
        maskPath1.addLine(to: pt3)
        maskPath1.addLine(to: pt4)
        maskPath1.addLine(to: pt5)
        maskPath1.addLine(to: pt6)
        maskPath1.close()

        maskLayer1.path = maskPath1.cgPath
        layer.mask = maskLayer1
    }
}

A couple problems with that approach though... the mask will not "auto-resize" when the button frame changes. So, you have to wait to call button.angledCorners() until auto-layout has finished setting the button's frame, and you have to call it again any time the frame changes.

If you have explicit frame sizes, that's not a problem.

But if you have buttons that change based on other views, or perhaps change size when the device is rotated, it's a hassle... and you probably want to make this a feature of a custom subclass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文