我添加了一个 UIView
。我有一个类 UIView
。
我想在其中绘制一个圆角矩形 UIBezierPath
,但这是否意味着我必须在我的类中覆盖 UIView
的 drawRect?
我想画这个:
但是我现在如何绘制简单的圆角矩形,在我的类 UIView
中我添加了这个函数:
func roundRect()
{
let rectWidth:CGFloat = 100
let rectHeight:CGFloat = 80
let xf:CGFloat = (self.frame.width - rectWidth) / 2
let yf:CGFloat = (self.frame.height - rectHeight) / 2
let ctx: CGContext = UIGraphicsGetCurrentContext()!
ctx.saveGState()
let rect = CGRect(x: xf, y: yf, width: rectWidth, height: rectHeight)
let clipPath: CGPath = UIBezierPath(roundedRect: rect, cornerRadius: rectCornerRadius).cgPath
ctx.addPath(clipPath)
ctx.closePath()
ctx.fillPath()
ctx.restoreGState()
}
最佳答案
在 View 中绘制简单形状的最简单和最有效的方法是向 View 添加一个CAShapeLayer
。您可以将它添加为 View 层的子层,或者您可以覆盖 View 的 layerClass()
方法以返回 CAShapeLayer
作为 View 的支持层的类。然后,您可以在 View 的 layoutSubviews()
方法
关于ios - 在 UIView 上绘制自定义圆角矩形 UIBezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42785599/