I am building a simple settings screen.
When then first setting is activated, the Speed Control appears. When it's turned off, the Speed Control disappears.
Based on what I know about SwiftUI, this should automatically animate based on the code below, but instead it just appears and disappears.
How can I make this animation nicer with SwiftUI, so it slides down from the cell above it, like in this presentation at 53:00?
import SwiftUI
import Combine
struct ContentView : View {
@ObjectBinding var settingsStore: SettingsStore
var body: some View {
NavigationView {
Form {
Toggle(isOn: $settingsStore.settingActivated.animation(.basic(duration: 3, curve: .easeInOut))) {
Text("Setting Activated")
}
if settingsStore.settingActivated {
VStack(alignment: .leading) {
Text("Speed Control")
HStack {
Image(systemName: "tortoise")
Slider(value: .constant(10), from: 0, through: 50, by: 1)
Image(systemName: "hare")
}
}.transition(.opacity)
}
}.navigationBarTitle(Text("Settings"))
}
}
}
class SettingsStore: BindableObject {
let didChange = PassthroughSubject<Void, Never>()
var settingActivated: Bool = UserDefaults.settingActivated {
didSet {
UserDefaults.settingActivated = settingActivated
didChange.send()
}
}
}
extension UserDefaults {
private struct Keys {
static let settingActivated = "SettingActivated"
}
static var settingActivated: Bool {
get {
return UserDefaults.standard.bool(forKey: Keys.settingActivated)
}
set {
UserDefaults.standard.set(newValue, forKey: Keys.settingActivated)
}
}
}
Form
withStack
, you will see that animations start to work. The only reason I can think for that behavior, is a bug. You should file one with Apple. – Romelda