【发布时间】:2017-08-04 18:22:43
【问题描述】:
我添加了一个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()
}
【问题讨论】:
-
根据您的具体规格,使用 resizableImageWithCapInsets 制作图像或使用 CAShapeLayer 可能是比覆盖 drawRect 更好的解决方案。
标签: ios iphone swift uiview uibezierpath