您正在绘制椭圆,其中心位于
Path
视图。为添加边框
路径
并发表评论
rotationEffect
和
position
亲眼看看。
Path { path in
let rect = CGRect(x: -semiMajor, y: -semiMinor, width: semiMajor * 2, height: semiMinor * 2)
path.addEllipse(in: rect)
}
.stroke(Color.red, lineWidth: 2)
.frame(width: semiMajor * 2, height: semiMinor * 2)
// add a border
.border(.blue)
// .rotationEffect(Angle(radians: angle))
// .position(x: centerX, y: centerY)
蓝色边框是“逻辑框架”
路径
视图,所以
旋转效应
和
位置
将对那个蓝色矩形进行操作。它将蓝色边框正确地转换到所需的位置。取消对这两行的注释,您将得到:
如果要使用以下命令定位椭圆
旋转效应
和
位置
这样,椭圆应该绘制在
路径
视图。这意味着您绘制椭圆的矩形应该以(0,0)为原点
Path { path in
let rect = CGRect(x: 0, y: 0, width: semiMajor * 2, height: semiMinor * 2)
path.addEllipse(in: rect)
}
.stroke(Color.red, lineWidth: 2)
.frame(width: semiMajor * 2, height: semiMinor * 2)
.border(.blue)
.rotationEffect(Angle(radians: angle))
.position(x: centerX, y: centerY)
这相当于只使用内置
Ellipse
形状:
Ellipse()
.stroke(Color.red, lineWidth: 2)
.frame(width: semiMajor * 2, height: semiMinor * 2)
.border(.blue)
.rotationEffect(Angle(radians: angle))
.position(x: centerX, y: centerY)
或者,您可以保留
路径
视图常数,仅转换路径的绘图指令。
Path { path in
let rect = CGRect(x: -semiMajor, y: -semiMinor, width: semiMajor * 2, height: semiMinor * 2)
path.addEllipse(in: rect)
}
.rotation(Angle(radians: angle), anchor: .topLeading)
.offset(x: centerX, y: centerY)
.stroke(Color.red, lineWidth: 2)
我用过这里
rotation
和
offset
,这两者都转换了路径的绘图指令,而不是路径的“逻辑框架”
路径
视图。“逻辑框架”
路径
视图将始终填充所有可用空间。
anchor: .topLeading
需要围绕(0,0)旋转视图,而不是默认的路径中心。