如何绘制 UIView 的单个圆角。
你好!我想在 UIView
上绘制一个圆角。 只有一个,其他无法更改。
Hi! I want to draw a rounded corner of my UIView
. Only one, others cannot be changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 iOS 3.2 开始,您可以使用 UIBezierPath 的功能来创建开箱即用的圆角矩形(其中仅指定的角是圆角的)。然后,您可以将其用作
CAShapeLayer
的路径,并将其用作视图图层的遮罩:就是这样 - 无需在 Core Graphics 中手动定义形状,无需在 Photoshop 中创建遮罩图像。该层甚至不需要失效。应用圆角或更改为新角就像定义新的 UIBezierPath 并使用其 CGPath 作为遮罩层的路径一样简单。
bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:
方法的corners
参数是一个位掩码,因此可以通过对多个角进行“或”运算来将它们圆角化。注意 - 与 CALayer renderInContext 方法结合使用时,图层蒙版将不会渲染。如果您需要使用此功能,请尝试使用以下圆角:只有两个圆角?。
Starting in iOS 3.2, you can use the functionality of
UIBezierPath
s to create an out-of-the-box rounded rect (where only corners you specify are rounded). You can then use this as the path of aCAShapeLayer
, and use this as a mask for your view's layer:And that's it - no messing around manually defining shapes in Core Graphics, no creating masking images in Photoshop. The layer doesn't even need invalidating. Applying the rounded corner or changing to a new corner is as simple as defining a new
UIBezierPath
and using itsCGPath
as the mask layer's path. Thecorners
parameter of thebezierPathWithRoundedRect:byRoundingCorners:cornerRadii:
method is a bitmask, and so multiple corners can be rounded by ORing them together.NOTE - Layer masks will not render when used in combination with the CALayer renderInContext method. If you need to use this try rounding corners with the following: Just two rounded corners?.
我按照 StuDev 的答案做了一个方法:
Example of using on a imageview in a UITableViewCell:
感谢 StuDev 提供了一个很好的解决方案!
I made a method of StuDev's answer:
Example of usage on a imageview in a UITableViewCell:
Thanks to StuDev for a great solution!
上面的更新答案,您不需要返回并管理它。可以在该函数中完成。
An updated answer to above , you dont need to return and manage that. It can be done in that function.
在做了一些小的研发之后,我制作了一个用于制作自定义角半径的函数,如下所示:
I have made a function for make the Custom corner radius after doing some small R&D as following: