在UIView上绘制自定义的圆角矩形UIBezierPath
问题描述:
我添加了一个UIView
。我有一个这样的课UIView
。在UIView上绘制自定义的圆角矩形UIBezierPath
我想绘制一个圆角的矩形UIBezierPath
它,但这是否意味着我必须重写我的类中的drawRect为UIView
?
我魔杖得出这样的:
但我现在该怎么画一些简单的圆角矩形,在我UIView
类我将此FUNC:
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()
}
答
最简单,最有效的方法在视图中绘制简单的形状是向视图添加CAShapeLayer
。您可以将其添加为视图图层的子图层,也可以覆盖该视图的layerClass()
方法以返回CAShapeLayer
作为视图背景层的类。然后你会安装想要抽入层的路径属性在视图的layoutSubviews()
方法
根据您的确切规格的路径,使图像与resizableImageWithCapInsets,或使用CAShapeLayer可能是一个更好的解决方案,然后重写的drawRect。 –