Skip to content

Instantly share code, notes, and snippets.

@CodeSlicing
Last active January 25, 2020 00:57
Show Gist options
  • Save CodeSlicing/23f4b2b6968f4ffe0ad0525160f43481 to your computer and use it in GitHub Desktop.
Save CodeSlicing/23f4b2b6968f4ffe0ad0525160f43481 to your computer and use it in GitHub Desktop.
Demo showing how to use GradientMap to modify the background of a picker to indicate severity of selection
//
// PickerSeverityDemo.swift
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Created by Adam Fordyce on 23/01/2020.
// Copyright © 2020 Adam Fordyce. All rights reserved.
//
import SwiftUI
import PureSwiftUITools
private let gradientMap = GradientMap([.pureGreen, .pureYellow, .pureRed], withResolution: 10)
struct PickerSeverityDemo: View {
var choices = 0..<10
@State private var choice = 5
var body: some View {
VStack {
Picker("Severity Level", selection: self.$choice) {
ForEach(choices) { choice in
Text((choice + 1).asString)
.width(50)
.backgroundColor(gradientMap[choice, default: .white])
.clipRoundedRectangleWithStroke(5, .black, lineWidth: 2)
}
}
.labelsHidden()
.scale(2)
}
}
}
struct PickerSeverityDemo_Previews: PreviewProvider {
struct PickerSeverityDemo_Harness: View {
var body: some View {
PickerSeverityDemo()
}
}
static var previews: some View {
PickerSeverityDemo_Harness()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment