gpt4 book ai didi

swift - UIBezierPath - 为单个路径绘制多个矩形

转载 作者:搜寻专家 更新时间:2023-11-01 06:26:48 25 4
gpt4 key购买 nike

我可以画一个矩形:

let roundedRect = UIBezierPath(roundedRect: rect, cornerRadius: 50)

有了这个我就不能在同一个路径上添加一个新的矩形。 (我需要多个矩形到一个层)

我也可以通过移动点来绘制一个矩形:

    path.move(to: CGPoint(x:point1.x-rectWidth/2.0,y:point1.y) )
path.addLine(to: CGPoint(x: point1.x-rectWidth/2.0, y: point2.y))
path.addLine(to: CGPoint(x: point1.x+rectWidth/2.0, y: point2.y))
path.addLine(to: CGPoint(x: point1.x+rectWidth/2.0, y: point1.y))
path.addLine(to: CGPoint(x:point1.x-rectWidth/2.0,y:point1.y))

这不会是一个圆角矩形。

我可以使用哪种方法来获得圆角矩形?

最佳答案

自己构建一个圆角矩形有点棘手。我建议使用 UIBezierPath 初始值设定项 init(roundedRect:cornerRadius:)

创建圆角矩形路径

您可以使用 append(_:) 将圆角矩形路径附加到另一个路径:

var path = UIBezierPath() //Create an empty path

//Add a rounded rect to the path
path.append(UIBezierPath(roundedRect: rect1, cornerRadius: radius1))

//Add another rounded rect to the path
path.append(UIBezierPath(roundedRect: rect2, cornerRadius: radius2))

//Lather, rinse, repeat.
path.append(UIBezierPath(roundedRect: rect3, cornerRadius: radius3))

编辑:

要使所有从底部绘制的矩形动画化,就像喷墨打印机一样,创建一个与 View 大小相同的 CAShapeLayer,在底部安装一个零高度矩形,并使该层成为你的 View 层。然后创建矩形的 CABasicAnimation 增长到 View 的整个高度。

关于swift - UIBezierPath - 为单个路径绘制多个矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53255720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com