也许这是一个愚蠢的问题,但是我试图将转换应用于像这样简单的路径:
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上还很新,所以我想我在这方面缺少重要的一步了吗? 谢谢大家!
答案 0 :(得分:0)
尝试一下并记住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))
答案 1 :(得分:0)
使用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