Skip to content

Instantly share code, notes, and snippets.

@SpectralDragon
Created February 3, 2020 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpectralDragon/45f166953447ff604b595144dfa8a70c to your computer and use it in GitHub Desktop.
Save SpectralDragon/45f166953447ff604b595144dfa8a70c to your computer and use it in GitHub Desktop.
iOS-like toggle style for SwiftUI. Because in MacOS we have another style for toggle
//
// iOSToggleStyle.swift
//
// Created by Vladislav Prusakov on 03.02.2020.
// Copyright © 2020 Vladislav Prusakov. All rights reserved.
//
import SwiftUI
struct iOSToggleStyle: ToggleStyle {
let width: CGFloat = 50
@Environment(\.pixelLength) private var pixelLength
func makeBody(configuration: Self.Configuration) -> some View {
HStack {
configuration.label
ZStack(alignment: configuration.isOn ? .trailing : .leading) {
RoundedRectangle(cornerRadius: 18)
.frame(width: width, height: width / 1.7)
.foregroundColor(configuration.isOn ? .rouge : .white)
.overlay(RoundedRectangle(cornerRadius: 18)
.stroke(configuration.isOn ? Color.rouge : Color.black.opacity(0.1),
lineWidth: self.pixelLength))
Circle()
.frame(width: (width / 2), height: width / 2)
.overlay(Circle().stroke(Color.black.opacity(0.1), lineWidth: self.pixelLength / 2))
.padding(2)
.foregroundColor(.white)
.shadow(color: Color.black.opacity(0.05), radius: 3, x: 0, y: 3)
.shadow(color: Color.black.opacity(0.1), radius: 2, x: 0, y: 2)
.shadow(color: Color.black.opacity(0.05), radius: 1, x: 0, y: 3)
}
.onTapGesture {
withAnimation {
configuration.$isOn.wrappedValue.toggle()
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment