Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. struct ContentView: View {
  2. @State var count: Int = 3
  3.  
  4. var body: some View {
  5. return NavigationView {
  6. VStack(spacing: 50) {
  7. HStack {
  8. Button(action: { self.count += 1 }) {
  9. Text("Add")
  10. }
  11.  
  12. Button(action: { self.count -= 1 }) {
  13. Text("Remove")
  14. }
  15. }
  16.  
  17. Circular {
  18. ForEach(0..<max(0, count)) { i in
  19. Color.green
  20. .frame(width: 30, height: 30)
  21. .clipShape(Circle())
  22. .overlay(Text("\(i)").font(.headline).foregroundColor(.white).allowsTightening(true))
  23. .shadow(radius: 3)
  24. }
  25. }
  26. }.navigationBarTitle(Text("Full Circle"))
  27. }
  28. }
  29. }
  30.  
  31. struct Circular<Items: View>: View {
  32. let items: [AnyView]
  33. let radius: Double = 100
  34.  
  35. init<D, C0: View>(@ViewBuilder content: () -> Items) where Items == ForEach<D, C0> {
  36. let fe = content()
  37. self.items = fe.data.map { AnyView(fe.content($0.identifiedValue)) }
  38. }
  39.  
  40. var body: some View {
  41. return ZStack {
  42. ForEach(0..<items.count) { idx in
  43. self.items[idx].modifier(self.positionModifier(at: idx))
  44. }
  45. }
  46. .background(Circle().foregroundColor(.gray).opacity(0.2))
  47. .frame(width: Length(2*radius), height: Length(2*radius))
  48. .animation(.fluidSpring())
  49. }
  50.  
  51. func positionModifier(at index: Int) -> _PositionLayout {
  52. let r = (2*Double.pi / Double(items.count)) * Double(index)
  53. return _PositionLayout(position: CGPoint(x: sin(r)*radius+radius, y: cos(r)*radius+radius))
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement