Created
August 18, 2020 02:21
-
-
Save jboullianne/3ab639fbe7ed32ed7df08cfc4b74a164 to your computer and use it in GitHub Desktop.
Custom SwiftUI ToggleStyle - Image Toggle
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
import SwiftUI | |
struct ImageToggleStyle: ToggleStyle { | |
var onImageName: String | |
var offImageName: String | |
func makeBody(configuration: Configuration) -> some View { | |
HStack { | |
configuration.label | |
Spacer() | |
Image(configuration.isOn ? onImageName : offImageName) | |
.resizable() | |
.aspectRatio(contentMode: .fill) | |
.frame(width: 51, height: 31, alignment: .center) | |
.overlay( | |
Circle() | |
.foregroundColor(.white) | |
.padding(.all, 3) | |
.offset(x: configuration.isOn ? 11 : -11, y: 0) | |
.animation(Animation.linear(duration: 0.1)) | |
).cornerRadius(20) | |
.onTapGesture { configuration.isOn.toggle() } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment