Skip to content

Instantly share code, notes, and snippets.

@lanserxt
Created December 3, 2025 12:54
Show Gist options
  • Select an option

  • Save lanserxt/c4eecabfb8f7d9272a8b3d88b61cabc1 to your computer and use it in GitHub Desktop.

Select an option

Save lanserxt/c4eecabfb8f7d9272a8b3d88b61cabc1 to your computer and use it in GitHub Desktop.
Swift Bits: SwiftUI - Animate Binding
import SwiftUI
struct SwiftBits: View {
//Some mode
enum PickerMode {
case left, right
}
@State private var mode: PickerMode = .left
var body: some View {
VStack {
//Button to toggle the mode
Button {
withAnimation {
mode = (mode != .left) ? .left : .right
}
} label: {
Text("Change mode")
.font(.title)
}
//Picker to toggle the mode
Picker("Picker", selection: $mode.animation()) {
Text("Left").tag(PickerMode.left)
Text("Right").tag(PickerMode.right)
}
.pickerStyle(SegmentedPickerStyle())
.padding()
if mode == .left {
Text("Left")
.transition(.opacity.combined(with:.scale))
} else {
Text("Right")
.transition(.opacity.combined(with:.scale))
}
}
}
}
#Preview {
SwiftBits()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment