Created
August 18, 2020 02:20
-
-
Save jboullianne/48647214d51900dd50dc6f2b5ca6d58d to your computer and use it in GitHub Desktop.
Custom SwiftUI ToggleStyle - Power Button
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 PowerToggleStyle: ToggleStyle { | |
func makeBody(configuration: Configuration) -> some View { | |
HStack { | |
configuration.label | |
Spacer() | |
Rectangle() | |
.foregroundColor(configuration.isOn ? .green : .gray) | |
.frame(width: 51, height: 31, alignment: .center) | |
.overlay( | |
Circle() | |
.foregroundColor(.white) | |
.padding(.all, 3) | |
.overlay( | |
GeometryReader { geo in | |
Path { p in | |
if !configuration.isOn { | |
p.addRoundedRect(in: CGRect(x: 20, y: 10, width: 10.5, height: 10.5), cornerSize: CGSize(width: 7.5, height: 7.5), style: .circular, transform: .identity) | |
} else { | |
p.move(to: CGPoint(x: 51/2, y: 10)) | |
p.addLine(to: CGPoint(x: 51/2, y: 31-10)) | |
} | |
}.stroke(configuration.isOn ? Color.green : Color.gray, lineWidth: 2) | |
} | |
) | |
.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