ContentView.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // ContentView.swift
  3. // ACarouselDemo iOS
  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: 50)
  25. ACarousel(items,
  26. spacing: spacing,
  27. headspace: headspace,
  28. sidesScaling: sidesScaling,
  29. isWrap: isWrap,
  30. autoScroll: autoScroll ? .active(time) : .inactive) { item in
  31. item.image
  32. .resizable()
  33. .scaledToFill()
  34. .frame(height: 300)
  35. .cornerRadius(30)
  36. }
  37. .frame(height: 300)
  38. Spacer()
  39. ControlPanel(spacing: $spacing,
  40. headspace: $headspace,
  41. sidesScaling: $sidesScaling,
  42. isWrap: $isWrap,
  43. autoScroll: $autoScroll,
  44. duration: $time)
  45. Spacer()
  46. }
  47. }
  48. }
  49. struct ControlPanel: View {
  50. @Binding var spacing: CGFloat
  51. @Binding var headspace: CGFloat
  52. @Binding var sidesScaling: CGFloat
  53. @Binding var isWrap: Bool
  54. @Binding var autoScroll: Bool
  55. @Binding var duration: TimeInterval
  56. var body: some View {
  57. VStack {
  58. Group {
  59. HStack {
  60. Text("spacing: ").frame(width: 120)
  61. Slider(value: $spacing, in: 0...30, minimumValueLabel: Text("0"), maximumValueLabel: Text("30")) { EmptyView() }
  62. }
  63. HStack {
  64. Text("headspace: ").frame(width: 120)
  65. Slider(value: $headspace, in: 0...30, minimumValueLabel: Text("0"), maximumValueLabel: Text("30")) { EmptyView() }
  66. }
  67. HStack {
  68. Text("sidesScaling: ").frame(width: 120)
  69. Slider(value: $sidesScaling, in: 0...1, minimumValueLabel: Text("0"), maximumValueLabel: Text("1")) { EmptyView() }
  70. }
  71. HStack {
  72. Toggle(isOn: $isWrap, label: {
  73. Text("wrap: ").frame(width: 120)
  74. })
  75. }
  76. VStack {
  77. HStack {
  78. Toggle(isOn: $autoScroll, label: {
  79. Text("autoScroll: ").frame(width: 120)
  80. })
  81. }
  82. if autoScroll {
  83. HStack {
  84. Text("duration: ").frame(width: 120)
  85. Slider(value: $duration, in: 1...10, minimumValueLabel: Text("1"), maximumValueLabel: Text("10")) { EmptyView() }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. .padding([.horizontal, .bottom])
  92. }
  93. }
  94. struct ContentView_Previews: PreviewProvider {
  95. static var previews: some View {
  96. ContentView()
  97. }
  98. }