Created
November 25, 2019 01:50
-
-
Save rudrankriyam/b687a459cffdd7c08293e0fd7b813580 to your computer and use it in GitHub Desktop.
Custom Stepper with Haptic Feedback in SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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