ContentView.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // ContentView.swift
  3. // ACarouselDemo macOS
  4. //
  5. // Created by Autumn on 2020/11/16.
  6. //
  7. import SwiftUI
  8. import ACarousel
  9. struct Item: Identifiable {
  10. let id = UUID()
  11. let image: Image
  12. }
  13. let roles = ["Luffy", "Zoro", "Sanji", "Nami", "Usopp", "Chopper", "Robin", "Franky", "Brook"]
  14. struct ContentView: View {
  15. let items: [Item] = roles.map { Item(image: Image($0)) }
  16. @State var spacing: CGFloat = 10
  17. @State var headspace: CGFloat = 10
  18. @State var sidesScaling: CGFloat = 0.8
  19. @State var isWrap: Bool = false
  20. @State var autoScroll: Bool = false
  21. @State var time: TimeInterval = 1
  22. var body: some View {
  23. VStack {
  24. Spacer().frame(height: 30)
  25. ACarousel(items,
  26. index: .constant(2),
  27. spacing: spacing,
  28. headspace: headspace,
  29. sidesScaling: sidesScaling,
  30. isWrap: isWrap,
  31. autoScroll: autoScroll ? .active(time) : .inactive) { item in
  32. item.image
  33. .resizable()
  34. .scaledToFill()
  35. .frame(height: 300)
  36. .cornerRadius(30)
  37. }
  38. .frame(height: 300)
  39. Spacer()
  40. ControlPanel(spacing: $spacing,
  41. headspace: $headspace,
  42. sidesScaling: $sidesScaling,
  43. isWrap: $isWrap,
  44. autoScroll: $autoScroll,
  45. duration: $time)
  46. Spacer()
  47. }
  48. }
  49. }
  50. struct ControlPanel: View {
  51. @Binding var spacing: CGFloat
  52. @Binding var headspace: CGFloat
  53. @Binding var sidesScaling: CGFloat
  54. @Binding var isWrap: Bool
  55. @Binding var autoScroll: Bool
  56. @Binding var duration: TimeInterval
  57. var body: some View {
  58. VStack {
  59. Group {
  60. HStack {
  61. Text("spacing: ").frame(width: 120)
  62. Slider(value: $spacing, in: 0...30, minimumValueLabel: Text("0"), maximumValueLabel: Text("30")) { EmptyView() }
  63. }
  64. HStack {
  65. Text("headspace: ").frame(width: 120)
  66. Slider(value: $headspace, in: 0...30, minimumValueLabel: Text("0"), maximumValueLabel: Text("30")) { EmptyView() }
  67. }
  68. HStack {
  69. Text("sidesScaling: ").frame(width: 120)
  70. Slider(value: $sidesScaling, in: 0...1, minimumValueLabel: Text("0"), maximumValueLabel: Text("1")) { EmptyView() }
  71. }
  72. HStack {
  73. Toggle(isOn: $isWrap, label: {
  74. Text("wrap: ").frame(width: 120)
  75. })
  76. }
  77. VStack {
  78. HStack {
  79. Toggle(isOn: $autoScroll, label: {
  80. Text("autoScroll: ").frame(width: 120)
  81. })
  82. }
  83. if autoScroll {
  84. HStack {
  85. Text("duration: ").frame(width: 120)
  86. Slider(value: $duration, in: 1...10, minimumValueLabel: Text("1"), maximumValueLabel: Text("10")) { EmptyView() }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. .padding([.horizontal, .bottom])
  93. }
  94. }
  95. struct ContentView_Previews: PreviewProvider {
  96. static var previews: some View {
  97. ContentView()
  98. }
  99. }