如何使用UibezierPath绘制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.
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
But I want a sharp cut.Like first image
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要绘制该形状,您需要使用
maskPath1.move(to:pt)
,然后使用一系列maskPath1.addline(TO:pt)
。您需要六分:
因此您可以这样编码:
尽管如此,该方法的几个问题...当按钮框架更改时,掩码不会“自动加强”。因此,您必须等待调用
button.angledCorners()
,直到自动layout完成设置按钮的框架为止,您必须在框架更改时再次调用它。如果您的明确框架大小,那不是问题。
但是,如果您的按钮会根据其他视图更改,或者可能在旋转设备时更改尺寸,那是一个麻烦……并且您可能希望使其成为自定义子类的功能。
To draw that shape, you want to use
maskPath1.move(to: pt)
and then a series ofmaskPath1.addLine(to: pt)
.You'll need six points:
so you could code it like this:
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.