|
| 1 | +import SwiftUI |
| 2 | +import SwiftUIExtensions |
| 3 | + |
| 4 | +struct HorizontalSliderExamplesView: View { |
| 5 | + @EnvironmentObject var model: Model |
| 6 | + |
| 7 | + var body: some View { |
| 8 | + ScrollView { |
| 9 | + Group { |
| 10 | + |
| 11 | + HSlider(value: $model.value1) |
| 12 | + |
| 13 | +// ValueSlider(value: $model.value1) |
| 14 | +// .background(Color.yellow) |
| 15 | +// .valueSliderStyle( |
| 16 | +// HorizontalValueSliderStyle( |
| 17 | +// track: { HorizontalValueTrack(value: $0) }, |
| 18 | +// thumbSize: CGSize(width: 32, height: 32), |
| 19 | +// options: .interactiveTrack |
| 20 | +// ) |
| 21 | +// ) |
| 22 | + |
| 23 | + |
| 24 | + HSlider(value: $model.value2, |
| 25 | + configuration: .init( |
| 26 | + thumbSize: CGSize(width: 16, height: 32) |
| 27 | + ) |
| 28 | + ) |
| 29 | + |
| 30 | + HSlider(value: $model.value3, track: |
| 31 | + LinearGradient(gradient: Gradient(colors: [.red, .orange, .yellow, .green, .blue, .purple, .pink]), startPoint: .leading, endPoint: .trailing) |
| 32 | + .frame(height: 8) |
| 33 | + .cornerRadius(4) |
| 34 | + ) |
| 35 | + |
| 36 | + HSlider( |
| 37 | + value: $model.value4, |
| 38 | + track: |
| 39 | + HTrack( |
| 40 | + value: model.value4, |
| 41 | + view: Rectangle() |
| 42 | + .foregroundColor(.green) |
| 43 | + .frame(height: 8), |
| 44 | + configuration: .init( |
| 45 | + offsets: 8 |
| 46 | + ) |
| 47 | + ) |
| 48 | + .background(Color.white) |
| 49 | + .frame(height: 8) |
| 50 | + .cornerRadius(3) |
| 51 | + .overlay( |
| 52 | + Capsule().strokeBorder(Color.white.opacity(0.5), lineWidth: 1) |
| 53 | + ) |
| 54 | + .animation( |
| 55 | + .spring(response: 0.7, dampingFraction: 0.4) |
| 56 | + ), |
| 57 | + configuration: .init( |
| 58 | + thumbSize: CGSize(width: 16, height: 16) |
| 59 | + ) |
| 60 | + |
| 61 | + ) |
| 62 | + |
| 63 | + HSlider( |
| 64 | + value: $model.value5, |
| 65 | + track: |
| 66 | + LinearGradient(gradient: Gradient(colors: [.purple, .blue, .purple]), startPoint: .leading, endPoint: .trailing) |
| 67 | + .frame(height: 6) |
| 68 | + .cornerRadius(3), |
| 69 | + configuration: .init( |
| 70 | + thumbSize: CGSize(width: 48, height: 16) |
| 71 | + ) |
| 72 | + ) |
| 73 | + |
| 74 | + HSlider( |
| 75 | + value: $model.value6, |
| 76 | + track: |
| 77 | + ZStack { |
| 78 | + HTrack( |
| 79 | + value: model.value6, |
| 80 | + view: Rectangle().foregroundColor(.white).opacity(0.2), |
| 81 | + mask: Rectangle() |
| 82 | + ) |
| 83 | + |
| 84 | + HTrack( |
| 85 | + value: model.value6, |
| 86 | + view: LinearGradient(gradient: Gradient(colors: [.purple, .blue, .purple]), startPoint: .leading, endPoint: .trailing).opacity(0.8), |
| 87 | + mask: Rectangle() |
| 88 | + ) |
| 89 | + .overlay( |
| 90 | + Capsule().strokeBorder(Color.white.opacity(0.5), lineWidth: 2) |
| 91 | + ) |
| 92 | + .animation(.easeInOut(duration: 1.0)) |
| 93 | + } |
| 94 | + .background(Capsule().foregroundColor(Color.secondary.opacity(0.25))) |
| 95 | + .frame(height: 32) |
| 96 | + .cornerRadius(16), |
| 97 | + thumb: EmptyView(), |
| 98 | + configuration: .init( |
| 99 | + options: .interactiveTrack, |
| 100 | + thumbSize: .zero |
| 101 | + ) |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + Group { |
| 106 | + HorizontalRangeSlider(range: $model.range1) |
| 107 | + |
| 108 | + HRangeSlider( |
| 109 | + range: $model.range2, |
| 110 | + track: |
| 111 | + HRangeTrack( |
| 112 | + range: model.range2, |
| 113 | + view: Capsule().foregroundColor(.purple), |
| 114 | + mask: Rectangle(), |
| 115 | + configuration: .init( |
| 116 | + lowerOffset: 32, |
| 117 | + upperOffset: 48 |
| 118 | + ) |
| 119 | + ) |
| 120 | + .background(Capsule().foregroundColor(Color.purple.opacity(0.25))) |
| 121 | + .frame(height: 8), |
| 122 | + lowerThumb: Circle().foregroundColor(.purple), |
| 123 | + upperThumb: Circle().foregroundColor(.purple), |
| 124 | + configuration: .init( |
| 125 | + thumbSize: CGSize(width: 32, height: 32) |
| 126 | + ) |
| 127 | + ) |
| 128 | + |
| 129 | + HRangeSlider( |
| 130 | + range: $model.range3, |
| 131 | + track: |
| 132 | + HRangeTrack( |
| 133 | + range: model.range3, |
| 134 | + view: LinearGradient(gradient: Gradient(colors: [.red, .orange, .yellow, .green, .blue, .purple, .pink]), startPoint: .leading, endPoint: .trailing), |
| 135 | + configuration: .init( |
| 136 | + offsets: 32 |
| 137 | + ) |
| 138 | + ) |
| 139 | + .background(LinearGradient(gradient: Gradient(colors: [.red, .orange, .yellow, .green, .blue, .purple, .pink]), startPoint: .leading, endPoint: .trailing).opacity(0.25)) |
| 140 | + .frame(height: 32) |
| 141 | + .cornerRadius(16), |
| 142 | + lowerThumb: HalfCapsule().foregroundColor(.white).shadow(radius: 3), |
| 143 | + upperThumb: HalfCapsule().rotation(Angle(degrees: 180)).foregroundColor(.white).shadow(radius: 3), |
| 144 | + configuration: .init( |
| 145 | + thumbSize: CGSize(width: 32, height: 32) |
| 146 | + ) |
| 147 | + ) |
| 148 | + |
| 149 | + HRangeSlider( |
| 150 | + range: $model.range4, |
| 151 | + track: |
| 152 | + HRangeTrack( |
| 153 | + range: model.range4, |
| 154 | + view: LinearGradient(gradient: Gradient(colors: [.purple, .blue, .purple]), startPoint: .leading, endPoint: .trailing), |
| 155 | + mask: Rectangle(), |
| 156 | + configuration: .init( |
| 157 | + offsets: 16 |
| 158 | + ) |
| 159 | + ) |
| 160 | + .mask(Ellipse()) |
| 161 | + .background(Ellipse().foregroundColor(Color.secondary.opacity(0.25))) |
| 162 | + .overlay(Ellipse().strokeBorder(Color.white.opacity(0.5), lineWidth: 1)) |
| 163 | + .padding(.vertical, 8), |
| 164 | + configuration: .init( |
| 165 | + thumbSize: CGSize(width: 16, height: 64) |
| 166 | + ) |
| 167 | + ) |
| 168 | + .frame(height: 64) |
| 169 | + |
| 170 | + HRangeSlider( |
| 171 | + range: $model.range5, |
| 172 | + in: 0.0...1.0, |
| 173 | + step: 0.01, |
| 174 | + track: |
| 175 | + HRangeTrack( |
| 176 | + range: model.range5, |
| 177 | + view: LinearGradient(gradient: Gradient(colors: [.yellow, .orange, .red]), startPoint: .leading, endPoint: .trailing), |
| 178 | + mask: Rectangle(), |
| 179 | + configuration: .init( |
| 180 | + offsets: 32 |
| 181 | + ) |
| 182 | + ) |
| 183 | + .background(Color.secondary.opacity(0.25)) |
| 184 | + .cornerRadius(16), |
| 185 | + lowerThumb: HalfCapsule().foregroundColor(.white).shadow(radius: 3), |
| 186 | + upperThumb: HalfCapsule().rotation(Angle(degrees: 180)).foregroundColor(.white).shadow(radius: 3), |
| 187 | + configuration: .init( |
| 188 | + thumbSize: CGSize(width: 32, height: 64) |
| 189 | + ) |
| 190 | + |
| 191 | + ) |
| 192 | + .frame(height: 64) |
| 193 | + |
| 194 | + HRangeSlider( |
| 195 | + range: $model.range6, |
| 196 | + track: HRangeTrack( |
| 197 | + range: model.range6, |
| 198 | + view: |
| 199 | + ZStack { |
| 200 | + LinearGradient(gradient: Gradient(colors: [.blue, .red]), startPoint: .leading, endPoint: .trailing) |
| 201 | + VStack { |
| 202 | + Text("Any View") |
| 203 | + .font(.largeTitle) |
| 204 | + .foregroundColor(Color.white) |
| 205 | + Text("Place any view here and it will be masked to a selected value range") |
| 206 | + .font(.title) |
| 207 | + .foregroundColor(Color.white.opacity(0.5)) |
| 208 | + } |
| 209 | + }, |
| 210 | + mask: RoundedRectangle(cornerRadius: 10), |
| 211 | + configuration: .init( |
| 212 | + offsets: 8 |
| 213 | + ) |
| 214 | + ) |
| 215 | + .background(Color.secondary.opacity(0.25)), |
| 216 | + configuration: .init( |
| 217 | + thumbSize: CGSize(width: 8, height: 64) |
| 218 | + ) |
| 219 | + ) |
| 220 | + .cornerRadius(8) |
| 221 | + .frame(height: 128) |
| 222 | + } |
| 223 | + |
| 224 | + } |
| 225 | + .padding() |
| 226 | + .navigationBarTitle("Horizontal Sliders") |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | + |
| 231 | +struct HorizontalSliderExamplesView_Previews: PreviewProvider { |
| 232 | + static var previews: some View { |
| 233 | + HorizontalSliderExamplesView().environmentObject(Model.preview) |
| 234 | + } |
| 235 | +} |
0 commit comments