如何在SwiftUI中排列圆形以创建三位一体。

huangapple go评论64阅读模式
英文:

How to arrange Circles to make Trinity in SwiftUI

问题

使用

ZStack {
    Circle()
        .frame(width: width * 0.73, height: width * 0.2)
    
    Circle()
        .frame(width: width * 0.73, height: width * 0.2)
    
    Circle()
        .frame(width: width * 0.73, height: width * 0.2)
}

请不要关心细节,只关注圆的位置。因为如果我们将两个圆在y轴上分别偏移-10和10,并将第三个圆在x轴上向上移动10,它将不再完美居中。

请帮忙,如何使用圆形进行完美的三位一体排列的数学计算是什么?

英文:

The desired outcome

Using

ZStack {
    Circle()
        .frame(width: width * 0.73, height: width * 0.2)
    
    Circle()
        .frame(width: width * 0.73, height: width * 0.2)
    
    Circle()
        .frame(width: width * 0.73, height: width * 0.2)
}

Don't care about details here, but the position of circles. Because if we offset two circles one by -10 and other by 10 in y axis, and move up the third circle in x axis by 10, it no longer perfectly centers.

Please help, what's the math in forming a perfect trinity using Circles?

答案1

得分: 1

以下是翻译好的部分:

以下的代码在视图中显示了一个 `trinity`

struct ContentView: View {
    let radius = CGFloat(222)

    var body: some View {
        ZStack {
            Circle().stroke(lineWidth: 4)
                .offset(CGSize(width: -radius/4, height: radius/2))

            Circle().stroke(lineWidth: 4)

            Circle().stroke(lineWidth: 4)
                .offset(CGSize(width: radius/4, height: radius/2))
        }
         .frame(width: radius, height: radius)
    }
}

EDIT-1:

为了突出重要部分,`ZStack`  `.frame(width: radius, height: radius)`

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Trinity")
            TrinityView(radius: CGFloat(123))
        }
        .frame(width: 333, height: 333)
    }
}

struct TrinityView: View {
    let radius: CGFloat

    var body: some View {
        ZStack {
            Circle().stroke(lineWidth: 4).offset(x: -radius/4, y: radius/2)
                .foregroundColor(.red)
            Circle().stroke(lineWidth: 4)
                .foregroundColor(.blue)
            Circle().stroke(lineWidth: 4).offset(x: radius/4, y: radius/2)
                .foregroundColor(.green)
        }.frame(width: radius, height: radius)
    }
}
英文:

The following code displays a trinity in a view.

struct ContentView: View {
    let radius = CGFloat(222)

    var body: some View {
        ZStack {
            Circle().stroke(lineWidth: 4)
                .offset(CGSize(width: -radius/4, height: radius/2))
            
            Circle().stroke(lineWidth: 4)
            
            Circle().stroke(lineWidth: 4)
                .offset(CGSize(width: radius/4, height: radius/2))
        }
         .frame(width: radius, height: radius)
    }
}

EDIT-1:

to highlight the important part, the .frame(width: radius, height: radius) of the ZStack

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Trinity")
            TrinityView(radius: CGFloat(123))
        }
        .frame(width: 333, height: 333)
    }
}

struct TrinityView: View {
    let radius: CGFloat
    
    var body: some View {
        ZStack {
            Circle().stroke(lineWidth: 4).offset(x: -radius/4, y: radius/2)
                .foregroundColor(.red)
            Circle().stroke(lineWidth: 4)
                .foregroundColor(.blue)
            Circle().stroke(lineWidth: 4).offset(x: radius/4, y: radius/2)
                .foregroundColor(.green)
        }.frame(width: radius, height: radius)
    }
}

huangapple
  • 本文由 发表于 2023年6月9日 13:55:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定