【问题标题】:Drawing custom rounded rectangle UIBezierPath on UIView在 UIView 上绘制自定义圆角矩形 UIBezierPath
【发布时间】: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


【解决方案1】:

在视图中绘制简单形状的最简单、最有效的方法是将CAShapeLayer 添加到视图中。您可以将其添加为视图层的子层,也可以覆盖视图的layerClass() 方法以返回CAShapeLayer 作为视图支持层的类。然后,您需要在视图的 layoutSubviews() 方法中将要绘制的路径安装到图层的路径属性中

【讨论】:

    猜你喜欢
    • 2016-05-05
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    相关资源
    最近更新 更多