Skip to content

Instantly share code, notes, and snippets.

@rudrankriyam
Created November 25, 2019 01:50
Show Gist options
  • Save rudrankriyam/b687a459cffdd7c08293e0fd7b813580 to your computer and use it in GitHub Desktop.
Save rudrankriyam/b687a459cffdd7c08293e0fd7b813580 to your computer and use it in GitHub Desktop.
Custom Stepper with Haptic Feedback in SwiftUI
struct CustomStepper : View {
@Binding var value: Double
var textColor: Color
var colorName: String
var step = 1.0/255
var body: some View {
HStack {
Text(colorName + " \(Int(value * 255.0))").font(.system(.caption, design: .rounded))
.foregroundColor(textColor)
Spacer()
Button(action: {
if self.value > 0 {
self.value -= self.step
self.feedback()
}
}, label: {
Image(systemName: "minus.square")
.foregroundColor(value > 0 ? textColor : Color.gray)
})
Button(action: {
if self.value < 1 {
self.value += self.step
self.feedback()
}
}, label: {
Image(systemName: "plus.square")
.foregroundColor(value < 1 ? textColor : Color.gray)
})
}
}
func feedback() {
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment