Skip to content

Commit

Permalink
SoftSwitchToggleStyle Update
Browse files Browse the repository at this point in the history
Add disabled support for SoftSwitchToggleStyle.
  • Loading branch information
costachung committed Oct 31, 2024
1 parent 3790e15 commit aae74c3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 51 deletions.
8 changes: 7 additions & 1 deletion Sources/Neumorphic/SoftDynamicToggleStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import SwiftUI

public struct SoftDynamicToggleStyle<S: Shape> : ToggleStyle {

@Environment(\.isEnabled) private var isEnabled

var shape: S
var mainColor : Color
var textColor : Color
Expand Down Expand Up @@ -54,6 +56,7 @@ public struct SoftDynamicToggleStyle<S: Shape> : ToggleStyle {
.opacity(pressedEffect == .none ? 1 : (configuration.isOn ? 0 : 1) )
}
)
.opacity(isEnabled ? 1 : 0.3)
.onTapGesture {
withAnimation(.easeInOut(duration: 0.2)) {
configuration.isOn = !configuration.isOn
Expand All @@ -65,7 +68,9 @@ public struct SoftDynamicToggleStyle<S: Shape> : ToggleStyle {


public struct SoftSwitchToggleStyle : ToggleStyle {


@Environment(\.isEnabled) private var isEnabled

var tintColor : Color
var offTintColor : Color

Expand Down Expand Up @@ -100,6 +105,7 @@ public struct SoftSwitchToggleStyle : ToggleStyle {
.offset(x: configuration.isOn ? 15 : -15)
.animation(.easeInOut(duration: 0.2), value: configuration.isOn)
}
.opacity(isEnabled ? 1 : 0.3)
.onTapGesture {
withAnimation(.easeInOut(duration: 0.2)) {
configuration.isOn.toggle()
Expand Down
114 changes: 64 additions & 50 deletions neumorphic-examples/ios-example/SoftSwitchToggleDemoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,66 +20,80 @@ extension Text {
struct SoftSwitchToggleDemoView: View {
@State var toggleIsOn : Bool = false

var body: some View {
return ZStack {
Color.Neumorphic.main.edgesIgnoringSafeArea(.all)
VStack(spacing:8){
Text("Toggle")
.font(.title)
.fontWeight(.bold)

Text("System Default Toggle Button")
@ViewBuilder
fileprivate func demoView() -> some View {
VStack{
HStack {
Text("System Default Toggle")
.demoViewSectionTitle()

//System Toggle Button
HStack(spacing:15) {
Spacer()
}
//System Toggle Button
HStack {
Spacer()
VStack{
Toggle("Toggle", isOn: $toggleIsOn)
.toggleStyle(SwitchToggleStyle(tint: .red))
.labelsHidden()
Spacer()
.toggleStyle(SwitchToggleStyle(tint: .red))
.labelsHidden()
Text("Enabled")
}
.padding()

//softSwitchToggleStyle
Spacer()
VStack{
Toggle("Toggle", isOn: $toggleIsOn)
.toggleStyle(SwitchToggleStyle(tint: .red))
.labelsHidden()
.disabled(true)
Text("Disabled")
}
Spacer()
}
.padding()

//softSwitchToggleStyle
VStack {
Text("Neumorphic")
.demoViewSectionTitle()
Text("Soft Switch Toggle Style")
.demoViewSectionTitle()

HStack(spacing:15) {
Spacer()
Toggle("Toggle", isOn: .constant(false))
.softSwitchToggleStyle(tint: .green, labelsHidden: true)
Spacer()
}
.padding()

HStack(spacing:15) {
Spacer()
Toggle("Toggle", isOn: $toggleIsOn)
.softSwitchToggleStyle(tint: .green, labelsHidden: true)
Spacer()
}
.padding()
HStack(spacing:15) {
Spacer()
Toggle("Toggle", isOn: $toggleIsOn)
.softSwitchToggleStyle(tint: .blue, labelsHidden: true)
Spacer()
}
.padding()
HStack(spacing:15) {
Spacer()
Toggle("Toggle", isOn: $toggleIsOn)
.softSwitchToggleStyle(tint: .red, labelsHidden: true)
Spacer()
softTogglePairView(tint: .gray)
softTogglePairView(tint: .green)
softTogglePairView(tint: .blue)
softTogglePairView(tint: .red)
}
.padding()
}
.padding()
}

@ViewBuilder
fileprivate func softTogglePairView(tint: Color = .green) -> some View {
HStack(spacing:15) {
Spacer()
VStack{
Toggle("Toggle", isOn: $toggleIsOn)
.softSwitchToggleStyle(tint: tint, labelsHidden: true)
Text("Enabled")
}
Spacer()
VStack{
Toggle("Toggle", isOn: $toggleIsOn)
.softSwitchToggleStyle(tint: tint, labelsHidden: true)
.disabled(true)
Text("Disabled")
}
Spacer()
}
}

var body: some View {
ZStack {
Color.Neumorphic.main.edgesIgnoringSafeArea(.all)
NavigationView {
ScrollView {
demoView()
}
.navigationTitle("SoftSwitchToggle")
.padding()


}
.padding()
}
}

Expand Down

0 comments on commit aae74c3

Please sign in to comment.