SwiftUI bezier路径位置改变屏幕旋转?



我想在屏幕旋转时保持我的BezierPath位置和对齐方式。下面是一些绘制两条路径的示例代码。我希望第二条路径绘制在第一条路径的顶部,这似乎发生在屏幕横向旋转时,而不是在我的设备上屏幕垂直旋转时。

是否有一种方法可以设置位置,使它们始终显示在相同的位置并相互对齐?

struct TestView: View {
var body: some View {
ZStack{
//I would like these to keep the same location/alignment to each other
ScaledBezier(bezierPath: .logo).stroke(lineWidth: 1.0)
ScaledBezier(bezierPath: .logo).stroke(lineWidth: 1.0).scaleEffect(2)
}
}
}
//Code to draw a path
struct ScaledBezier: Shape {
let bezierPath: UIBezierPath
func path(in rect: CGRect) -> Path {
let path = Path(bezierPath.cgPath)
// Figure out how much bigger we need to make our path in order for it to fill the available space without clipping.
let multiplier = min(rect.width, rect.height)
// Create an affine transform that uses the multiplier for both dimensions equally.
let transform = CGAffineTransform(scaleX: multiplier, y: multiplier)
// Apply that scale and send back the result.
return path.applying(transform)
}
}
extension UIBezierPath {
/// The Unwrap logo as a Bezier path.
static var logo: UIBezierPath {
let path = UIBezierPath()
path.move(to: CGPoint(x: 0.534, y: 0.5816))
path.addCurve(to: CGPoint(x: 0.1877, y: 0.088), controlPoint1: CGPoint(x: 0.534, y: 0.5816), controlPoint2: CGPoint(x: 0.2529, y: 0.4205))
path.addCurve(to: CGPoint(x: 0.9728, y: 0.8259), controlPoint1: CGPoint(x: 0.4922, y: 0.4949), controlPoint2: CGPoint(x: 1.0968, y: 0.4148))
path.addCurve(to: CGPoint(x: 0.0397, y: 0.5431), controlPoint1: CGPoint(x: 0.7118, y: 0.5248), controlPoint2: CGPoint(x: 0.3329, y: 0.7442))
path.addCurve(to: CGPoint(x: 0.6211, y: 0.0279), controlPoint1: CGPoint(x: 0.508, y: 1.1956), controlPoint2: CGPoint(x: 1.3042, y: 0.5345))
path.addCurve(to: CGPoint(x: 0.6904, y: 0.3615), controlPoint1: CGPoint(x: 0.7282, y: 0.2481), controlPoint2: CGPoint(x: 0.6904, y: 0.3615))
return path
}
}

我尝试用.position(x: 0, y: 0)设置位置,但是路径在屏幕旋转时不能保持对齐。

谢谢你的帮助!

我只需要将绘图转换设置为一个常量值而不是屏幕大小。

struct ScaledBezier: Shape {
let bezierPath: UIBezierPath
func path(in rect: CGRect) -> Path {
let path = Path(bezierPath.cgPath)

//set to constant
let multiplier = min(1000.0, 1000.0)

//constant transform
let transform = CGAffineTransform(multiplier, 0.0, 0.0, multiplier, 0.0, 0.0)
// Apply that scale and send back the result.
return path.applying(transform)
}
}

最新更新