对 UIBezierPath 应用转换会使绘图消失

Applying a transformation to UIBezierPath makes drawing disappear

也许这是一个愚蠢的问题,但我正在尝试将转换应用于像这样简单的路径:

let path = UIBezierPath(rect: CGRect(x: 10, y: 10, width: 20, height: 20))
path.apply(CGAffineTransform(rotationAngle: 2.0))
"colorLiteral".setFill()
path.fill()

添加 "apply" 线似乎完全擦除了视图上的形状,而如果我不包括那条线,形状会正确显示在它的位置。

我在 Swift 还很陌生,所以我想我错过了这方面的一个重要步骤? 谢谢大家!

试试这个,记住 rotationAngle 需要 弧度

let bounds = CGRect(x: 10, y: 10, width: 20, height: 20)
let path = UIBezierPath(rect: bounds)
path.apply(CGAffineTransform(translationX: -bounds.midX, y: -bounds.midY))
path.apply(CGAffineTransform(rotationAngle: CGFloat.pi / 4))
path.apply(CGAffineTransform(translationX: bounds.midX, y: bounds.midY))

使用 CATransform3D

shapeLayer.transform = CATransform3DMakeScale(1, -1, 1)

变换路径,

let shapeBounds = shapeLayer.bounds
let mirror = CGAffineTransform(scaleX: 1,
                                  y: -1)
let translate = CGAffineTransform(translationX: 0,
                                  y: shapeBounds.size.height)
let concatenated = mirror.concatenating(translate)

bezierPath.apply(concatenated)

shapeLayer.path = bezierPath.cgPath

变换层,

let shapeFrame = CGRect(x: -120, y: 120, width: 350, height: 350)
let mirrorUpsideDown = CGAffineTransform(scaleX: 1,
                                              y: -1)
shapeLayer.setAffineTransform(mirrorUpsideDown)
shapeLayer.frame = shapeFrame