Created
December 3, 2025 12:54
-
-
Save lanserxt/c4eecabfb8f7d9272a8b3d88b61cabc1 to your computer and use it in GitHub Desktop.
Swift Bits: SwiftUI - Animate Binding
This file contains hidden or 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 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