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

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

How to arrange Circles to make Trinity in SwiftUI

问题

使用

  1. ZStack {
  2. Circle()
  3. .frame(width: width * 0.73, height: width * 0.2)
  4. Circle()
  5. .frame(width: width * 0.73, height: width * 0.2)
  6. Circle()
  7. .frame(width: width * 0.73, height: width * 0.2)
  8. }

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

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

英文:

The desired outcome

Using

  1. ZStack {
  2. Circle()
  3. .frame(width: width * 0.73, height: width * 0.2)
  4. Circle()
  5. .frame(width: width * 0.73, height: width * 0.2)
  6. Circle()
  7. .frame(width: width * 0.73, height: width * 0.2)
  8. }

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

以下是翻译好的部分:

  1. 以下的代码在视图中显示了一个 `trinity`
  2. struct ContentView: View {
  3. let radius = CGFloat(222)
  4. var body: some View {
  5. ZStack {
  6. Circle().stroke(lineWidth: 4)
  7. .offset(CGSize(width: -radius/4, height: radius/2))
  8. Circle().stroke(lineWidth: 4)
  9. Circle().stroke(lineWidth: 4)
  10. .offset(CGSize(width: radius/4, height: radius/2))
  11. }
  12. .frame(width: radius, height: radius)
  13. }
  14. }
  15. EDIT-1:
  16. 为了突出重要部分,`ZStack` `.frame(width: radius, height: radius)`
  17. struct ContentView: View {
  18. var body: some View {
  19. VStack {
  20. Text("Trinity")
  21. TrinityView(radius: CGFloat(123))
  22. }
  23. .frame(width: 333, height: 333)
  24. }
  25. }
  26. struct TrinityView: View {
  27. let radius: CGFloat
  28. var body: some View {
  29. ZStack {
  30. Circle().stroke(lineWidth: 4).offset(x: -radius/4, y: radius/2)
  31. .foregroundColor(.red)
  32. Circle().stroke(lineWidth: 4)
  33. .foregroundColor(.blue)
  34. Circle().stroke(lineWidth: 4).offset(x: radius/4, y: radius/2)
  35. .foregroundColor(.green)
  36. }.frame(width: radius, height: radius)
  37. }
  38. }
英文:

The following code displays a trinity in a view.

  1. struct ContentView: View {
  2. let radius = CGFloat(222)
  3. var body: some View {
  4. ZStack {
  5. Circle().stroke(lineWidth: 4)
  6. .offset(CGSize(width: -radius/4, height: radius/2))
  7. Circle().stroke(lineWidth: 4)
  8. Circle().stroke(lineWidth: 4)
  9. .offset(CGSize(width: radius/4, height: radius/2))
  10. }
  11. .frame(width: radius, height: radius)
  12. }
  13. }

EDIT-1:

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

  1. struct ContentView: View {
  2. var body: some View {
  3. VStack {
  4. Text("Trinity")
  5. TrinityView(radius: CGFloat(123))
  6. }
  7. .frame(width: 333, height: 333)
  8. }
  9. }
  10. struct TrinityView: View {
  11. let radius: CGFloat
  12. var body: some View {
  13. ZStack {
  14. Circle().stroke(lineWidth: 4).offset(x: -radius/4, y: radius/2)
  15. .foregroundColor(.red)
  16. Circle().stroke(lineWidth: 4)
  17. .foregroundColor(.blue)
  18. Circle().stroke(lineWidth: 4).offset(x: radius/4, y: radius/2)
  19. .foregroundColor(.green)
  20. }.frame(width: radius, height: radius)
  21. }
  22. }

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:

确定